android – 如何禁用AlertDialog内的按钮?

android – 如何禁用AlertDialog内的按钮?,第1张

概述我正在尝试写一个具有3个按钮的AlertDialog.如果不满足某个条件,我想要中间的中性按钮被禁用. 这是我的代码: int playerint = settings.getPlayerInt();int monsterint = settings.getMonsterInt(); AlertDialog.Builder alertbox = new AlertDialo 我正在尝试写一个具有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",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内的按钮?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址:https://54852.com/web/1132436.html

(0)
打赏 微信扫一扫微信扫一扫 支付宝扫一扫支付宝扫一扫
上一篇 2022-05-30
下一篇2022-05-30

发表评论

登录后才能评论

评论列表(0条)

    保存