【问题标题】:How to prevent dismiss of alert dialog box when user click in background of it?当用户单击它的背景时,如何防止关闭警报对话框?
【发布时间】:2016-04-09 09:44:44
【问题描述】:

你好朋友我做了一个警告对话框,当没有网络连接时触发,当人们点击确定按钮时,他将被定向到 No Internet.class 它工作正常,但真正的问题是当用户点击背景时警报对话框自动关闭。我怎样才能阻止它? 我的意思是取消对话框。我也使用了这个方法 SetCancelable(false);但它不起作用。它给出了一个错误。

下面是我的代码

AlertDialog.Builder alert = new AlertDialog.Builder(this,R.style.AppCompatAlertDialogStyle);
        alert.setTitle("Internet Problem");
        alert.setMessage("No Internet!!!");

       // alert.SetCancelable(false);

        alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                //Do something here where "ok" clicked and then perform intent from activity context
                Intent intent = new Intent(BaseActivity.this, NoInternet.class);
                BaseActivity.this.startActivity(intent);

            }
        });

【问题讨论】:

标签: java android


【解决方案1】:

只需添加alert.setCancelable(false);。它不起作用的原因(来自代码中的注释)是您之前使用了错误的方法名称。

AlertDialog.Builder alert = new AlertDialog.Builder(...);

    alert.setCancelable(false); // not SetCancelable(false). Case sensitive

});

【讨论】:

    【解决方案2】:
      Dialog dialog = new Dialog(context)
      dialog.setCanceledOnTouchOutside(false);
    

    【讨论】:

      【解决方案3】:

      使用setCancelable(false) 方法。希望对您有所帮助。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-02-20
        • 2022-01-06
        • 2020-02-04
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多