你如何禁用AlertDialog内部的按钮? [英] How do you disable a button inside of an AlertDialog?

查看:128
本文介绍了你如何禁用AlertDialog内部的按钮?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想写出具有3个按钮的AlertDialog。我想中间,中间按钮,如果没有满足一定的条件下被禁用。

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

下面是我的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);

        }
    }

该行:Button按钮=((AlertDialog)alertbox).getButton(AlertDialog.BUTTON_NEUTRAL);  给人的错误:无法从AlertDialog.Builder转换为      AlertDialog

The line: Button button = ((AlertDialog) alertbox).getButton(AlertDialog.BUTTON_NEUTRAL); gives the error: Cannot cast from AlertDialog.Builder to AlertDialog

我该如何解决这个问题?我究竟做错了什么?

How do I fix this?? What am I doing wrong?

推荐答案

您不能调用 getButton() AlertDialog.Builder 。它创建之后被称为对结果 AlertDialog 。换句话说

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

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.

心连心

这篇关于你如何禁用AlertDialog内部的按钮?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆