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

时间:2022-12-25 19:12:37

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

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

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

原文:

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()方法?

 public void promptAdministratorPassword() {
AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setTitle("Alert!");
alert.setMessage("Please enter your password: "); // Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input); alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
password = input.getText().toString(); if (password.equals("password")) {
try {
compareKeys();
} catch (IOException | NoSuchAlgorithmException | InvalidKeySpecException e) {
e.printStackTrace();
}
}
dialog.dismiss();
}
}); alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
}

解决方案原文: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方法中。