2011-08-22 33 views
3

我想對一個活動做一個密碼提示框,所以一旦它有正確的答案,它會關閉對話框,但我無法找到一種方法,當我搜索如何關閉對話框的方式我已經編碼了。如何取消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(); 

有沒有辦法關閉這個alertbox?

回答

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

的方法解僱()是未定義的類型AlertDialog.Builder – ozmank

+0

的方法取消()也是未定義類型AlertDialog.Builder – CodeWarrior

+0

創建對話框爲對象,即: 決賽AlertDialog對話框; AlertDialog.Builder alert = new AlertDialog.Builder(this); dialog = alert.create(); 然後你可以調用dialog.cancel(); –

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

你想對話框當用戶按下一個按鈕消失?如果是這樣,你不必做任何事情,應該自動解除(當用戶按任何按鈕時)。

當您輸入正確的密碼時,您希望對話框自行消失嗎?然後,你將需要一個TextWatcher添加到EditText上域:在TextWatcher

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

回調函數將被稱爲每當文本的變化,你可以在那裏檢查密碼是否正確,如果是,調用

dialog.dismiss(); 
5

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

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