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

时间:2012-11-23 09:59:06

标签: android dialog android-alertdialog

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

任何建议都将不胜感激。

4 个答案:

答案 0 :(得分:23)

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

答案 1 :(得分:6)

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

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

希望它有所帮助!

答案 2 :(得分:2)

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

答案 3 :(得分:2)

只是

  public static void SendAppleNotification()
    {
        // Configuration (NOTE: .pfx can also be used here)
        var config = new ApnsConfiguration(ApnsConfiguration.ApnsServerEnvironment.Sandbox, "C:\\Users\\MancMiniUser\\Documents\\visual studio 2013\\Projects\\PushSharpDemo\\PushSharpDemo\\Resources\\P12CertifyPIE.p12", "1234");

        // Create a new broker
        var apnsBroker = new ApnsServiceBroker(config);

        // Wire up events
        apnsBroker.OnNotificationFailed += (notification, aggregateEx) =>
        {

            aggregateEx.Handle(ex =>
            {

                // See what kind of exception it was to further diagnose
                if (ex is ApnsNotificationException)
                {
                    var notificationException = (ApnsNotificationException)ex;

                    // Deal with the failed notification
                    var apnsNotification = notificationException.Notification;
                    var statusCode = notificationException.ErrorStatusCode;

                    Console.WriteLine("Apple Notification Failed: ID={apnsNotification.Identifier}, Code={statusCode}");

                }
                else
                {
                    // Inner exception might hold more useful information like an ApnsConnectionException           
                    Console.WriteLine("Apple Notification Failed for some unknown reason : {ex.InnerException}");
                }

                // Mark it as handled
                return true;
            });
        };

        apnsBroker.OnNotificationSucceeded += (notification) =>
        {
            Console.WriteLine("Apple Notification Sent!");
        };

        // Start the broker
        apnsBroker.Start();


        // Queue a notification to send
        apnsBroker.QueueNotification(new ApnsNotification
        {
            DeviceToken = "3cfcc2ceefe6675d89b063e2e39d03503b3917250e6743d185c755e9e2c3579d",
            Payload = JObject.Parse("{\"aps\":{\"alert\":\"Hello from sourabh v.4.0 Tester.\",\"badge\":\"1\"}}")

        });

        // Stop the broker, wait for it to finish   
        // This isn't done after every message, but after you're
        // done with the broker
        apnsBroker.Stop();
    }

问题解决了!!