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

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方法中。

 

转载于:https://www.cnblogs.com/hsit0318/p/lanqibing.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值