如何取消alertdialog?

时间:2011-08-22 09:03:15

标签: android dialog

我想在活动上做一个密码警告框,所以一旦它有正确的答案,就会关闭对话框,但我似乎无法找到一种方法来搜索如何以我编码的方式关闭对话框

这是我的代码

final AlertDialog.Builder alert1 = new AlertDialog.Builder(this);
alert1.setTitle("Password");
alert1.setMessage("Please enter your password below and press Ok.");

final EditText input = new EditText(this);
alert1.setView(input);

alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        String value = input.getText().toString().trim();
        ((Global) Menu.this.getApplication()).setgPassword(value);
        ((Global) Menu.this.getApplication()).setgSiteId(strSavedMem1);
        LogIn();
    }
});

alert1.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        System.exit(0);
    }
});

alert1.show();

有没有办法关闭此警报框?

8 个答案:

答案 0 :(得分:7)

您应该查看Android文档的此链接: http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog

在那里解释了如何取消对话框。

答案 1 :(得分:7)

您可以在DialogInterface onClick方法中接收对话框实例。

@Override
public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
}

答案 2 :(得分:0)

试试这样:

final AlertDialog.Builder alert1 = new AlertDialog.Builder(this);
                      alert1.setTitle("Password");
                      alert1.setMessage("Please enter your password below and press Ok.");
                      final EditText input = new EditText(this);
                      alert1.setView(input);
                      alert1.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int whichButton) {
                              String value = input.getText().toString().trim();
                              ((Global) Menu.this.getApplication()).setgPassword(value);
                              ((Global) Menu.this.getApplication()).setgSiteId(strSavedMem1);
                              LogIn();
                              alert1.dismiss();
                          }
                      });

                      alert1.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
                          public void onClick(DialogInterface dialog, int whichButton) {
                              //System.exit(0);
                              alert1.cancel();
                          }
                      });
                      alert1.show();

                }

答案 3 :(得分:0)

检查一下,

 AlertDialog.Builder successfullyLogin = new Builder(
        YourActivity.this);
      successfullyLogin.setCancelable(false);
      successfullyLogin.setMessage("Successfully LoggedIn !");

      successfullyLogin.setPositiveButton("Ok",
        new DialogInterface.OnClickListener() {
         @Override
         public void onClick(DialogInterface dialog,
           int which) {
          // TODO Auto-generated method stub

         }
        });
      successfullyLogin.show();

答案 4 :(得分:0)

当用户按下按钮时,您希望对话框消失吗?如果是这样,你不需要做任何事情,它应该被自动解除(当用户按下任何按钮时)。

当输入正确的密码时,您是否希望对话框自行消失?然后,您需要将TextWatcher添加到EditText字段:

input.addTextChangedListener(new TextWatcher()....)

TextWatcher中的回调函数将在文本发生变化时被调用,您可以检查密码是否正确,如果是,请调用

dialog.dismiss();

答案 5 :(得分:0)

你不必做任何事情。 正如在匿名链接的Android文档中所述: “当用户触摸使用AlertDialog.Builder创建的任何操作按钮时,系统会为您解除对话框。” http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog

答案 6 :(得分:0)

试试这个:

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


Dialog.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

      public void onClick(DialogInterface arg0, int arg1) {
        Dialog.setCancelable(true);

      }});

答案 7 :(得分:0)

我知道这个问题是很久以前问过的,但是与大多数其他答案相比,我有不同的解决方案。

  

就我而言,我想在单击展开视图中的某个按钮时取消AlertDialog。当然,这与setNegativeButton()或setPositiveButton()无关,因此我无权访问任何alertdialog对象。


这是我解决此问题的方法:

在显示AlertDialog(alertDialog.show())时,您可以将.show对象存储到变量中,如下所示:

AlertDialog shower = alertDialog.show();

完成此操作后,您可以通过在AlertDialog范围内的任何位置调用shower.cancel()来轻松取消Alertdialog。

我希望这会有所帮助。快活的编码!

相关问题