自定义Dialog设置setCancelable(false)不起作用,按返回键还是会关闭Dialog!
解决方案,设置 Dialog 的返回键监听,不关闭Dialog
/**
* 检测是否拥有写入系统权限的权限
*/
private void showDialog(){
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { //版本在23及以上
if (!Settings.System.canWrite(this)) {
final CommonDialog deleteDialog = new CommonDialog(mContext);
deleteDialog.setTitle("应用需要您授权\n\"修改系统设置\"的权限");
deleteDialog.setPositiveButton(R.string.confirm,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteDialog.cancel();
Intent intent = new Intent(Settings.ACTION_MANAGE_WRITE_SETTINGS,
Uri.parse("package:" + getPackageName()));
startActivity(intent);
}
});
deleteDialog.setNegativeButton(R.string.cancel,
new android.content.DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
deleteDialog.cancel();
finish();
}
});
deleteDialog.setCancelable(false); //返回键还是会关闭Dialog
deleteDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode==KeyEvent.KEYCODE_BACK && event.getRepeatCount()==0) {
return true; //返回true会截断返回键的点击事件,不做其他处理
} else {
return false;
}
}
});
deleteDialog.show();
}
}
}