簡體   English   中英

如何取消alertdialog?

[英]How to cancel an alertdialog?

我想在一個活動上做一個密碼警告框,所以一旦它有正確的答案就會關閉對話框,但我似乎無法找到一種方法來搜索如何以我編碼方式關閉對話框。

這是我的代碼

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

有沒有辦法關閉此警報框?

您可以在DialogInterface onClick方法中接收對話框實例。

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

您應該查看Android文檔的此鏈接: http//developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog

在那里解釋了如何取消對話。

你不必做任何事情。 正如在匿名鏈接的Android文檔中所述:“當用戶觸摸使用AlertDialog.Builder創建的任何操作按鈕時,系統會為您解除對話框。” http://developer.android.com/guide/topics/ui/dialogs.html#DismissingADialog

試試這個:

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


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

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

      }});

我知道很久以前就問過這個問題,但我從大多數其他答案中得到了不同的解決方案。

在我的情況下,我想在點擊膨脹視圖中的某個按鈕時取消AlertDialog。 當然,這與setNegativeButton()或setPositiveButton()無關,所以我無法訪問任何alertdialog對象。


以下是我解決這個問題的方法:

在顯示AlertDialog( alertDialog.show() )時,您可以將.show對象存儲到變量中,如下所示:

AlertDialog shower = alertDialog.show();

完成此操作后,您可以通過在AlertDialog范圍內的任何位置調用shower.cancel()輕松取消alertdialog。

我希望這有幫助。 快樂編碼!

試試這樣:

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

                }

檢查一下,

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

當用戶按下按鈕時,您希望對話框消失嗎? 如果是這樣,您不必做任何事情,它應該被自動解除(當用戶按下任何按鈕時)。

輸入正確的密碼后,您是否希望對話框自行消失? 然后,您需要將TextWatcher添加到EditText字段:

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

文本更改時將調用TextWatcher中的回調函數,您可以檢查密碼是否正確,如果是,則調用

dialog.dismiss();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM