
这是我的代码:
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",int arg1) { // This should not be static// createTrivia(); trivia(); } }); // Return to Last Saved CheckPoint alertBox.setNegativebutton("Run Away!",int arg1) { runAway(); } }); // show the alert Box alertBox.show();// Intellect Checkbutton button = ((AlertDialog) alertBox).getbutton(AlertDialog.button_NEUTRAL); if(monsterint > playerint) { button.setEnabled(false); } } 行:button button =((AlertDialog)alertBox).getbutton(AlertDialog.button_NEUTRAL);
给出错误:无法从AlertDialog.Builder转换为
AlertDialog
我该如何解决??我究竟做错了什么?
解决方法 您不能在AlertDialog.Builder上调用getbutton().创建后必须在结果AlertDialog上调用它.换一种说法AlertDialog.Builder alertBox = new AlertDialog.Builder(this);//...All your code to set up the buttons initiallyAlertDialog dialog = alertBox.create();button button = dialog.getbutton(AlertDialog.button_NEUTRAL);if(monsterint > playerint) { button.setEnabled(false);} 构建器只是使构建对话框更容易的类,它不是实际的对话框本身.
HTH
总结以上是内存溢出为你收集整理的android – 如何禁用AlertDialog内的按钮?全部内容,希望文章能够帮你解决android – 如何禁用AlertDialog内的按钮?所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)