【问题标题】:AlertDialog closing without clicking ok and cancel in Android 4.0在 Android 4.0 中,AlertDialog 关闭而不单击确定并取消
【发布时间】:2012-05-01 18:50:41
【问题描述】:

我编写了以下代码来显示 AlertDialogBox,如下所示

private void dispAlertBox(final String title,final String message,final String ok,final String cancel){

    final AlertDialog.Builder alertbox = new AlertDialog.Builder(this);

         try{
          alertbox.setMessage(message);
          alertbox.setTitle(title);
          alertbox.setPositiveButton(ok,new DialogInterface.OnClickListener(){
             public void onClick(DialogInterface arg0, int arg1){

          }
          });
          alertbox.setNegativeButton(cancel,new DialogInterface.OnClickListener(){
          public void onClick(DialogInterface arg0, int arg1){
          }
          });
          alertbox.show();
       }catch(Exception e){
         //Handle BadTokenException.
       }
} 

在上面我想在单击确定并取消时关闭 AlertDialog。因此,如果我们单击除 AlertDialog(确定并取消)以外的任何其他区域,它不应该关闭。它工作正常2.x 版本的 Android。但在 Android 4.0 中,即使我们单击 ok 和取消按钮以外的空白区域,对话框也会关闭。我该如何克服这个问题。

谢谢&问候,

文卡特。

【问题讨论】:

    标签: android android-alertdialog


    【解决方案1】:

    尝试添加

    alertbox.setCancelable(false);
    

    在展示之前。

    【讨论】:

      【解决方案2】:

      我认为您需要使用alertbox.setCanceledOnTouchOutside(false); 这应该可以防止对话框在用户触摸屏幕上的其他位置时关闭。另外,如果用户按下返回按钮,添加alertbox.setCancelable(false); 将阻止对话框关闭。

      【讨论】:

        猜你喜欢
        • 2017-11-29
        • 2013-01-27
        • 2014-11-23
        • 2014-02-23
        • 1970-01-01
        • 2017-10-24
        • 1970-01-01
        • 2021-11-27
        • 1970-01-01
        相关资源
        最近更新 更多