android - 为什么 setCanceledOnTouchOutside(false) 在警报生成器中不起作用?

标签 android dialog android-alertdialog

我的 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/

相关文章:

java - 找不到以下类: com. codecarpet.fbconnect.FBLoginButton

java - NetworkBoundResource 类中 MediatorLiveData 的使用

android - 在 onClick(DialogInterface v, int buttonId) 中获取上下文?

android - 单击时关闭 DialogFragment

android - 警报对话框 - 出现错误

android - 如何在android studio中导入colorPickerDialog库

android - "java.lang.RuntimeException: Could not launch intent"用于具有不确定 ProgressBar 的 UI

php - PHP while 循环中的 jquery 对话框

android - 如何区分 Longclicks 和 onclick 事件?

flutter - 仅在部分屏幕上显示“flutter ”对话框