为什么setCanceledOnTouchOutside(false)在"警报"构建器中不起作用?

Hes*_*sam 6 android dialog android-alertdialog

我的活动中有一个警告对话框,不希望用户通过单击对话框外部来解除它.根据我的研究(像这样)我找到了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:
        }
}
});
Run Code Online (Sandbox Code Playgroud)

任何建议将不胜感激.

sda*_*bet 24

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();
Run Code Online (Sandbox Code Playgroud)


key*_*bee 6

这是一个有趣的问题,我想我知道你的答案.

我一直在不同平台上测试应用程序,我发现它们之间存在细微差别.在触摸Toast消息时,在Android 4.0之上,它就会消失.我猜对于对话框(和AlertDialogs)是一样的.它只是在触摸时"消失"(但它没有被解雇! - 只是无法看到).

希望它有所帮助!