如何禁用AlertDialog内的按钮?

时间:2011-09-12 18:37:38

标签: java android android-alertdialog classcastexception

我正在尝试用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

我该如何解决这个问题?

3 个答案:

答案 0 :(得分:18)

您无法在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);
}

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

HTH

答案 1 :(得分:8)

我认为更好的解决方案:

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

答案 2 :(得分:4)

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

示例:

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