如何取消AlertDialog?

7

我希望能在一个活动中显示一个密码提醒框,一旦输入正确的答案就关闭对话框,但是我无法找到一种方法来关闭对话框,无论如何都没有成功。

这是我的代码:

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个回答

7
您可以在DialogInterface onClick方法中接收对话框实例。
@Override
public void onClick(DialogInterface dialog, int which) {
    dialog.dismiss();
}

7

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

      }});

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

                }

2
方法dismiss()对于类型AlertDialog.Builder未定义。 - ozmank
方法cancel()对于类型AlertDialog.Builder也未定义。 - CodeWarrior
将对话框创建为一个对象,例如: final AlertDialog dialog; AlertDialog.Builder alert = new AlertDialog.Builder(this); dialog = alert.create(); 然后,您可以调用dialog.cancel()。 - Casey Murray

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

0

你想让对话框在用户按下按钮时消失吗?如果是这样,你不需要做任何事情,它应该会自动消失(当用户按下任何按钮时)。

你想让对话框在输入正确密码后自动消失吗?那么你需要向EditText字段添加一个TextWatcher:

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

在TextWatcher中的回调函数将在文本更改时被调用,您可以在那里检查密码是否正确,如果正确,则调用

dialog.dismiss();

0

0

我知道这个问题很久以前就被问过了,但我有一个不同于其他答案的解决方案。

我的情况是,当充气视图内的某个按钮被点击时,我想取消AlertDialog。当然,这与setNegativeButton()或setPositiveButton()无关,因此我无法访问任何alertdialog对象。


以下是我的解决方法:

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

AlertDialog shower = alertDialog.show();

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

希望这能帮到您。愉快编程!


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接