如何禁用 AlertDialog 内的按钮?
我正在尝试编写一个带有 3 个按钮的 AlertDialog
。如果不满足特定条件,我希望禁用中间的中性按钮。
代码
int playerint = settings.getPlayerInt();
int monsterint = settings.getMonsterInt();
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("You have Encountered a Monster");
alertbox.setPositiveButton("Fight!",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
createMonster();
fight();
}
});
alertbox.setNeutralButton("Try to Outwit",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
// This should not be static
// createTrivia();
trivia();
}
});
// Return to Last Saved CheckPoint
alertbox.setNegativeButton("Run Away!",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
runAway();
}
});
// show the alert box
alertbox.show();
// Intellect Check
Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);
if(monsterint > playerint) {
button.setEnabled(false);
}
}
行:
Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);
给出错误:
无法从 AlertDialog.Builder 转换为 AlertDialog
如何解决此问题?
I am trying to write an AlertDialog
with 3 buttons. I want the middle, Neutral Button to be disabled if a certain condition is not met.
Code
int playerint = settings.getPlayerInt();
int monsterint = settings.getMonsterInt();
AlertDialog.Builder alertbox = new AlertDialog.Builder(this);
alertbox.setMessage("You have Encountered a Monster");
alertbox.setPositiveButton("Fight!",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
createMonster();
fight();
}
});
alertbox.setNeutralButton("Try to Outwit",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
// This should not be static
// createTrivia();
trivia();
}
});
// Return to Last Saved CheckPoint
alertbox.setNegativeButton("Run Away!",
new DialogInterface.OnClickListener() {
// do something when the button is clicked
public void onClick(DialogInterface arg0, int arg1) {
runAway();
}
});
// show the alert box
alertbox.show();
// Intellect Check
Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);
if(monsterint > playerint) {
button.setEnabled(false);
}
}
The line:
Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);
Gives error:
Cannot cast from AlertDialog.Builder to AlertDialog
How do I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您无法在
AlertDialog.Builder
上调用getButton()
。创建后必须在生成的AlertDialog
上调用它。换句话说,构建器只是一个使构建对话框更容易的类......它不是实际的对话框本身。
华泰
You can't call
getButton()
on theAlertDialog.Builder
. It has to be called on the resultingAlertDialog
after creation. In other wordsThe builder is just a class to make constructing the dialog easier...it isn't the actual dialog itself.
HTH
我认为更好的解决方案:
Better solution in my opinion:
诀窍是您需要使用由
AlertDialog.Builder.show()
方法返回的AlertDialog
对象。无需调用AlertDialog.Builder.create()
。例子:
The trick is that you need to use the
AlertDialog
object retuned byAlertDialog.Builder.show()
method. No need to callAlertDialog.Builder.create()
.Example: