Android   发布时间:2022-04-28  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了android – 如何禁用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",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 check

Button button = ((AlertDialog) alertBox).getButton(AlertDialog.bUTTON_NEUTRAL);

        if(monsterint > player@R_450_10185@ {


            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 initially

AlertDialog dialog = alertBox.create();
Button button = dialog.getButton(AlertDialog.bUTTON_NEUTRAL);
if(monsterint > player@R_450_10185@ {
    button.setEnabled(false);
}

构建器只是使构建对话框更容易的类,它不是实际的对话框本身.

HTH

大佬总结

以上是大佬教程为你收集整理的android – 如何禁用AlertDialog内的按钮?全部内容,希望文章能够帮你解决android – 如何禁用AlertDialog内的按钮?所遇到的程序开发问题。

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

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。