【发布时间】:2014-02-27 15:46:20
【问题描述】:
我有一个对话框供用户输入解锁码。如果他们输入了错误的代码,我想保持盒子打开,以便他们有机会修复它。
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Unlock Code");
alert.setMessage("Please enter the unlock code.");
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String value = input.getText().toString();
if(codeUnlock.checkCode(value)){ // checks the code that was put in
Toast.makeText(MainActivity.this, "Thank you for purchasing.", Toast.LENGTH_LONG).show();
yearPurchased = currentYear;
checkForUpdate(false);
}else{
Toast.makeText(MainActivity.this, "Incorrect code.", Toast.LENGTH_LONG).show();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
}
});
alert.show();
基本上,如果 checkCode 为 false,我只想显示 toast 但不关闭窗口。
有什么建议吗?
谢谢。
【问题讨论】:
-
你不能通过警告对话框来做到这一点。 AlertDialog 中的所有
set*Button都会关闭对话框。您需要使用带有您自己的布局和按钮的对话框。 -
你试过setCancelable(false)吗?
-
为什么不制作自定义对话框类,扩展对话框,只有在密码正确时才调用 dismis()?
-
是的。所有好的建议。我希望有一个更简单的方法...