繁体   English   中英

如何禁用AlertDialog内的按钮?

[英]How do you disable a button inside of an 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",
                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

我该如何解决?

你不能在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 > playerint) {
    button.setEnabled(false);
}

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

HTH

我认为更好的解决方案:

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);
                    }
                }
            }
        });

诀窍是你需要使用AlertDialog.Builder.show()方法重新调整的AlertDialog对象。 无需调用AlertDialog.Builder.create()

例:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM