Android 按下后退时警告对话框消失?
Android Alert dialog is dismissing on Back press?
当我点击系统后退按钮时,我的警报对话框关闭,我不想关闭我的警报,我的代码在这里:-
fun shoWResultPopUp() {
if (!isFinishing()) {
val mBuilder = AlertDialog.Builder(this@ExamActivity)
val mView = layoutInflater.inflate(R.layout.show_result_popup, null)
val mResult = mView.findViewById(R.id.resultBtn) as Button
mBuilder.setView(mView)
val dialog = mBuilder.create()
dialog.setCanceledOnTouchOutside(false);
dialog.show()
mResult.setOnClickListener {
val intent = Intent(this, ResultActivity::class.java)
startActivity(intent)
}
}
}
override fun onBackPressed() {
return
}
您需要设置:
dialog.setCancelable(false)
根据此方法的 Android 文档:
Sets whether this dialog is cancelable with the
{@link KeyEvent#KEYCODE_BACK BACK} key.
所以这正是您想要的情况。
注:
dialog.setCanceledOnTouchOutside 是为了 preventing/allowing 弹出窗口在弹出窗口外触摸时关闭,而不是后退按钮。文档:
Sets whether this dialog is canceled when touched outside the window's
bounds. If setting to true, the dialog is set to be cancelable if not
already set.
你可以查看这个link
Dismiss Dialog onBackPressAndroid
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
MyActivity.this.finish();
}
});
当我点击系统后退按钮时,我的警报对话框关闭,我不想关闭我的警报,我的代码在这里:-
fun shoWResultPopUp() {
if (!isFinishing()) {
val mBuilder = AlertDialog.Builder(this@ExamActivity)
val mView = layoutInflater.inflate(R.layout.show_result_popup, null)
val mResult = mView.findViewById(R.id.resultBtn) as Button
mBuilder.setView(mView)
val dialog = mBuilder.create()
dialog.setCanceledOnTouchOutside(false);
dialog.show()
mResult.setOnClickListener {
val intent = Intent(this, ResultActivity::class.java)
startActivity(intent)
}
}
}
override fun onBackPressed() {
return
}
您需要设置:
dialog.setCancelable(false)
根据此方法的 Android 文档:
Sets whether this dialog is cancelable with the {@link KeyEvent#KEYCODE_BACK BACK} key.
所以这正是您想要的情况。
注:
dialog.setCanceledOnTouchOutside 是为了 preventing/allowing 弹出窗口在弹出窗口外触摸时关闭,而不是后退按钮。文档:
Sets whether this dialog is canceled when touched outside the window's bounds. If setting to true, the dialog is set to be cancelable if not already set.
你可以查看这个link Dismiss Dialog onBackPressAndroid
dialog.setOnCancelListener(new DialogInterface.OnCancelListener()
{
@Override
public void onCancel(DialogInterface dialog)
{
MyActivity.this.finish();
}
});