2011-08-22 65 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(); 
}