【发布时间】:2014-02-11 15:24:59
【问题描述】:
在我的应用程序中,我有一个功能可以检查来自显示的 AlertDialog 输入的文本和输入文本。如果文本等于字符串变量,则返回 True,否则返回 False,并捕获此结果值以继续条件代码。
但是这样做似乎有点困难,因为我在其他帖子中读到过,询问如何解决同样的问题。
我已经这样做了:
private boolean checkAdministratorPassword() {
final enterPasswordResult[0] = false;
AlertDialog.Builder alert = new AlertDialog.Builder(mContext);
alert.setTitle("Confirm action");
alert.setIcon(R.drawable.ic_launcher);
alert.setMessage("Enter administrator pass to continue");
final EditText input = new EditText(mContext);
input.setPadding(5, 0, 5, 0);
alert.setView(input);
alert.setPositiveButton("Accept", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String strPass = input.getEditableText().toString();
if (strPass.length() == 0) {
dialog.cancel();
}
if (strPass.equalsIgnoreCase(Constantes.ADMIN_PASS)) {
enterPasswordResult[0] = true;
dialog.cancel();
} else {
Toast.makeText(mContext, "Invalid pass..!!", Toast.LENGTH_LONG).show();
dialog.cancel();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
dialog.cancel();
}
});
AlertDialog alertDialog = alert.create();
alertDialog.show();
return enterPasswordResult[0];
}
我这样调用函数:
If ( checkAdministratorPassword() == True ){
//true conditions
}
但问题是检查功能不会等待结果继续执行代码,它只是自行继续,我没有得到适当的行为。
【问题讨论】:
-
“If”和“True”是复制/粘贴错误,还是在您的代码中确实如此?
-
checkAdministratorPassword(new Runnable(){ public void run() { /*code for true*/ }}, new Runnable(){ public void run() { /*code for false*/ }})...checkAdministratorPassword(Runnable trueCode, Runnable falseCode)在警报的 onClick 中执行trueCode.run();如果密码正常,falseCode.run();如果不是 -
这是一个关于你的应用程序结构的问题:我认为最好在正按钮 onClick 监听器中调用所需的函数
-
@codeMagic:是的,它只是一个复制/粘贴错误。在我的代码中,我正确地使用了“if”和“true”。谢谢你-
-
你可以试试这个解决方案检查一下here
标签: android android-alertdialog