在默认情况下禁用AlertDialog的(正)按钮。

时间:2022-02-06 18:55:25

How do you disable the positive button of an Android AlertDialog by default?

如何在默认情况下禁用Android AlertDialog的正数按钮?

It appears to be quite normal to want the positive button (In this case "Save") to be disabled before the user has made a change to the view (In this case an EditText.

在用户对视图(在本例中是EditText)进行更改之前,想要禁用正数按钮(在本例中是“Save”)似乎是很正常的。

I know that I can get the button by calling dialog.getButton(DialogInterface.BUTTON_POSITIVE) but this call will return null if show() hasn't been called yet.

我知道可以通过调用dialog.getButton(dialog. button_positive)获取按钮,但是如果show()还没有被调用,这个调用将返回null。

2 个解决方案

#1


12  

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        if(condition)
        ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    }
});

dialog.show();

#2


4  

You have to call show() to access alert dialog's buttons. So, just after you call show() on alertDialog, you get the negative button and set it disabled like this:

您必须调用show()来访问警报对话框的按钮。所以,在你调用alertDialog上的show()之后,你就会得到一个否定按钮,并将其设置为如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
            .setTitle("Title")
            .setMessage("Message")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert);
    AlertDialog d = builder.show();
    d.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);

So, its negative button becomes disabled by default.

因此,它的负按钮在默认情况下被禁用。

#1


12  

AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setPositiveButton(android.R.string.ok, null);
AlertDialog dialog = builder.create();
dialog.setOnShowListener(new OnShowListener() {

    @Override
    public void onShow(DialogInterface dialog) {
        if(condition)
        ((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);
    }
});

dialog.show();

#2


4  

You have to call show() to access alert dialog's buttons. So, just after you call show() on alertDialog, you get the negative button and set it disabled like this:

您必须调用show()来访问警报对话框的按钮。所以,在你调用alertDialog上的show()之后,你就会得到一个否定按钮,并将其设置为如下所示:

AlertDialog.Builder builder = new AlertDialog.Builder(getContext())
            .setTitle("Title")
            .setMessage("Message")
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {

                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert);
    AlertDialog d = builder.show();
    d.getButton(AlertDialog.BUTTON_NEGATIVE).setEnabled(false);

So, its negative button becomes disabled by default.

因此,它的负按钮在默认情况下被禁用。