java 如何禁用 AlertDialog 内的按钮?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7392321/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 19:45:59  来源:igfitidea点击:

How do you disable a button inside of an AlertDialog?

javaandroidandroid-alertdialogclasscastexception

提问by Frank Bozzo

I am trying to write an AlertDialogwith 3 buttons. I want the middle, Neutral Button to be disabled if a certain condition is not met.

我正在尝试AlertDialog用 3 个按钮编写一个。如果不满足某个条件,我希望中间的 Neutral Button 被禁用。

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

无法从 AlertDialog.Builder 转换为 AlertDialog

How do I fix this?

我该如何解决?

回答by Devunwired

You can't call getButton()on the AlertDialog.Builder. It has to be called on the resulting AlertDialogafter creation. In other words

你不能打电话getButton()AlertDialog.Builder。必须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 > playerint) {
    button.setEnabled(false);
}

The builder is just a class to make constructing the dialog easier...it isn't the actual dialog itself.

构建器只是一个使构建对话框更容易的类……它不是实际的对话框本身。

HTH

HTH

回答by Michal

Better solution in my opinion:

我认为更好的解决方案:

AlertDialog.Builder builder = new AlertDialog.Builder(context); 
builder.setPositiveButton("Positive", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int whichButton) {
        // some code
    }
});
AlertDialog alertDialog = builder.create();
alertDialog.setOnShowListener(new DialogInterface.OnShowListener() {
            @Override
            public void onShow(DialogInterface dialog) {
                if(**some condition**)
                {
                    Button button = alertDialog.getButton(AlertDialog.BUTTON_POSITIVE);
                    if (button != null) {
                        button.setEnabled(false);
                    }
                }
            }
        });

回答by Ashraf Sousa

The trick is that you need to use the AlertDialogobject retuned by AlertDialog.Builder.show()method. No need to call AlertDialog.Builder.create().

诀窍是您需要使用AlertDialog通过AlertDialog.Builder.show()方法重新调整的对象。不需要打电话AlertDialog.Builder.create()

Example:

例子:

        AlertDialog dialog = alertbox.show();
        if(monsterint > playerint) {
            Button button = dialog.getButton(AlertDialog.BUTTON_NEUTRAL);
            button.setEnabled(false);
        }