Stack Overflow 排错翻译  - Closing AlertDialog.Builder in Android -Android环境中关闭AlertDialog.Builder

转自:http://www.lanqibing.com/archives/783.html

 

原文:

In the following code, I tried to dismiss the AlertDialog box but to no avail. However, if I remove compareKeys() function, the dismiss will work. So how can I make it dismiss after calling the compareKeys() function?

翻译:

在下面的代码中,我尝试去释放AlertDialog对话框,但释放无效。然而,我去掉 compareKeys()方法后,释放AlertDialog是正常工作的。我如何在释放AlertDialog对话框后正常调用compareKeys()方法?

 1 public void promptAdministratorPassword() {
 2     AlertDialog.Builder alert = new AlertDialog.Builder(this);
 3 
 4     alert.setTitle("Alert!");
 5     alert.setMessage("Please enter your password: ");
 6 
 7     // Set an EditText view to get user input
 8     final EditText input = new EditText(this);
 9     alert.setView(input);
10 
11     alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
12         public void onClick(DialogInterface dialog, int whichButton) {
13             password = input.getText().toString();
14 
15             if (password.equals("password")) {
16                 try {
17                     compareKeys();
18                 } catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
19                     e.printStackTrace();
20                 }
21             }
22             dialog.dismiss();
23         }
24     });
25 
26     alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
27         public void onClick(DialogInterface dialog, int whichButton) {
28             // Canceled.
29         }
30     });
31     alert.show();
32 }

 

解决方案原文:Call dialog.dismiss() before password = input.getText().toString() and add dialog.dismiss() inside setNegativeButton's OnClickListener too.

解决方案翻译:将password = input.getText().toString()放到调用dialog.dismiss()之前,并将dialog.dismiss()放到setNegativeButton的OnClickListener方法中。

 

 posted on 2016-12-13 20:07  hsit0318  阅读(418)  评论(0编辑  收藏  举报