如何禁用警报对话框中的按钮?
2022-09-05 00:18:30
我正在尝试用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
如何解决此问题?