我的 Activity 中有一个警告对话框,不希望用户可以通过在对话框外单击来关闭它。根据我的研究 ( like this ),我找到了 setCanceledOnTouchOutside(false);
方法。但是,我无法在我的应用程序中使用它,并且在我使用此方法时可以关闭对话框。
这是我的代码:
private AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setTitle("");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (intAlertAction) {
case 1:
case 2:
case 3:
default:
}
}
});
如有任何建议,我们将不胜感激。
最佳答案
setCanceledOnTouchOutside
仅防止通过在对话框外部单击来关闭。但是您仍然可以使用后退按钮关闭它。
如果您根本不想让您的对话框可取消,请使用 dialog.setCancelable(false)
我刚刚测试了您的(已修复)代码,它按预期工作:用户在单击退出时无法关闭对话框。试一试:
AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setTitle("");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
}
});
alertDialog.show();
关于android - 为什么 setCanceledOnTouchOutside(false) 在警报生成器中不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13526690/