【问题标题】:why setCanceledOnTouchOutside(false) doesn't work in Alert builder?为什么 setCanceledOnTouchOutside(false) 在警报生成器中不起作用?
【发布时间】:2012-11-11 16:15:27
【问题描述】:

我的活动中有一个警报对话框,不希望用户通过单击对话框外部来关闭它。根据我的研究 (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:
        }
}
});

任何建议将不胜感激。

【问题讨论】:

标签: android dialog android-alertdialog


【解决方案1】:

在您的AlertDialog 中添加setCancelable(false),例如:

AlertDialog alertDialog;
alertDialog = new AlertDialog.Builder(this).create();
alertDialog.setCanceledOnTouchOutside(false);
alertDialog.setCancelable(false);
alertDialog.setTitle("");
alertDialog.setButton(AlertDialog.BUTTON_NEUTRAL, "OK",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int which) {
            }
        });
alertDialog.show();

【讨论】:

  • alertDialog.setCancelable(false);已添加
【解决方案2】:

只需添加dialog.setCancelable(false) 即可禁用返回按钮。

【讨论】:

    【解决方案3】:

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

    我一直在不同平台上测试应用程序,我注意到它们之间存在细微差别。在 android 4.0 以上,当您触摸 Toast 消息时,它就会消失。我想对话框(和 AlertDialogs)也是如此。触摸时它只是“消失”(但不会消失!-只是看不到)。

    希望对您有所帮助!

    【讨论】:

    • 谢谢 Keybee,是的,你是对的。我在 android 2.3.3 上运行了上面的代码,结果和我预期的一样。在 Android 4.1 中对话框关闭时,我无法通过关闭对话框来关闭对话框。 Android 是 bl.sht :(
    • 只需使用 alertDialog.setCancelable(false);它会工作..:) @Hesam
    【解决方案4】:

    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();
    

    【讨论】:

    • 感谢提琴手,但基于抱怨 :) 我的客户不希望通过单击对话框外部来关闭对话框。因此,后退按钮目前不是问题。另一件事,我认为上述方法适用于对话框(基于接受的答案,我没有测试它)但它在 AlertDialog 中不起作用。
    • 我不明白你。 1)您是否希望在单击外部时关闭对话框? 2)您希望在点击返回按钮时关闭对话框吗?
    • 我有 AlertDialog 而不是 Dialog。基于我的编码的区别在于对话框中的两种方法(dialog.setCancelable(false) 和 dialog.setCanceledOnTouchOutside(false);) 工作正常。但是在 AlertDialog 中这些都不起作用。该请求是不要让用户通过点击对话框外部来关闭对话框(在我的情况下是警报对话框)。希望能解释清楚,对不起我的英语很糟糕。
    • 我刚刚用 AlertDialog 测试了代码,它运行良好。查看我的编辑
    • 哇,我没有发现你的代码和我的代码有任何区别。我正在 Android v4.1 上进行测试。和你测试的安卓版本一样吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-03-03
    • 1970-01-01
    • 2021-10-01
    • 2016-09-07
    • 2013-02-17
    • 1970-01-01
    相关资源
    最近更新 更多