加入收藏 | 设为首页 | 会员中心 | 我要投稿 PHP编程网 - 黄冈站长网 (http://www.0713zz.com/)- 数据应用、建站、人体识别、智能机器人、语音技术!
当前位置: 首页 > 教程 > 正文

Android Activity 常用方法

发布时间:2021-11-15 14:41:30 所属栏目:教程 来源:互联网
导读:Android Activity 常用技巧 1.设置 Activity 背景色为透明 在style.xml里面声明: style name=TranslucentActivityStyle parent=@android:style/Theme.Translucent item name=android:windowIsTranslucenttrue/item item name=android:windowNoTitletrue/ite

Android Activity 常用技巧
 
1.设置 Activity 背景色为透明
 
在style.xml里面声明:
 
<style name="TranslucentActivityStyle" parent="@android:style/Theme.Translucent">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/transparent</item>
<item name="android:windowAnimationStyle">@null</item>
</style>
在Activity的onCreate方法中添加:  
 
setTheme(R.style.Theme_Transparent);
注意,此代码需要放在setContentView方法之前。后续是需要在AndroidMainfest的相应Activity里面声明:
 
android:theme="@style/Theme.Transparent"
2. 如何安全退出已调用多个 Activity 的 Application?
①. 记录打开的Activity,每打开一个Activity,就记录下来,在需要退出的时候,关闭每一个activity。
 
②. 发送特定的广播,在需要结束应用时,发送一个特定的广播,每一个Activity收到广播后关闭。
 
③. 通过 Intent 的 flag 来实现。实现intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP) 调用一个activity。此时如果该任务栈中已经有该 Activity,那么系统会把这个 Activity 上面的所有 Activity 干掉。其实相当于给 Activity 配置的启动模式为 SingleTask。
 
3.显示和隐藏输入法
private void showKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.showSoftInput(mUrlText, InputMethodManager.SHOW_IMPLICIT);
}
 
private void hideKeyboard() {
    InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
    imm.hideSoftInputFromWindow(mUrlText.getWindowToken(), 0);
}
4. Android 6.0以上动态检测请求权限
//检查权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
         || ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
    ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE}, REQUEST_CODE);
}
5. Android 获取相册图片
Intent innerIntent = new Intent();
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.KITKAT) {
   innerIntent.setAction(Intent.ACTION_GET_CONTENT);
} else {
    innerIntent.setAction(Intent.ACTION_OPEN_DOCUMENT);
}
innerIntent.setType("image/*");
Intent wrapperIntent = Intent.createChooser(innerIntent, "选择图片");
this.startActivityForResult(wrapperIntent, REQUEST_CODE);
 
 
public String uri2FilePath(Uri uri) {
        String path = "";
        if (Build.VERSION.SDK_INT >= 19 && DocumentsContract.isDocumentUri(this, uri)) {
            String wholeID = DocumentsContract.getDocumentId(uri);
            String id = wholeID.split(":")[1];
            String[] column = { MediaStore.Images.Media.DATA };
            String sel = MediaStore.Images.Media._ID + "=?";
            Cursor cursor = getContentResolver().query(
                    MediaStore.Images.Media.EXTERNAL_CONTENT_URI, column, sel,
                    new String[] { id }, null);
            if (cursor != null) {
                int columnIndex = cursor.getColumnIndex(column[0]);
                if (cursor.moveToFirst()) {
                    path = cursor.getString(columnIndex);
                }
                cursor.close();
            }
        } else {
            String[] projection = { MediaStore.Images.Media.DATA };
            Cursor cursor = getContentResolver().query(uri, projection, null, null, null);
            if (cursor != null) {
                int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
                if (cursor.moveToFirst()) {
                    path = cursor.getString(column_index);
                }
                cursor.close();
            }
        }
        return path;
}

(编辑:PHP编程网 - 黄冈站长网)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    热点阅读