请查看下面的自定义对话框.我在对话框上有一个edittext字段,如果文本字段为空,我想禁用positiveButton.我可以为文本字段设置一个charListener,但我不确定如何将positivebutton设置为从该侦听器禁用或启用?正面和负面按钮的参考是什么?

 case DIALOG_TEXT_ENTRY:
    // This example shows how to add a custom layout to an AlertDialog
    LayoutInflater factory = LayoutInflater.from(this);
    final View textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);
    return new AlertDialog.Builder(AlertDialogSamples.this)
        .setIconAttribute(android.R.attr.alertDialogIcon)
        .setTitle(R.string.alert_dialog_text_entry)
        .setView(textEntryView)
        .setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked OK so do some stuff */
            }
        })
        .setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface dialog, int whichButton) {
                /* User clicked cancel so do some stuff */
            }
        })
        .create();
}

推荐答案

Edit for complete solution...

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("This is the example code snippet to disable button if edittext attached to dialog is empty.");
builder.setPositiveButton("PositiveButton",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // DO TASK
            }
        });
builder.setNegativeButton("NegativeButton",
        new DialogInterface.OnClickListener() {
            public void onClick(DialogInterface arg0, int arg1) {
                // DO TASK
            }
        });

// Set `EditText` to `dialog`. You can add `EditText` from `xml` too.
final EditText input = new EditText(MainActivity.this);

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT
);
input.setLayoutParams(lp);


builder.setView(input);

final AlertDialog dialog = builder.create();
dialog.show();

// Initially disable the button
((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

// OR you can use here setOnShowListener to disable button at first time.

// Now set the textchange listener for edittext
input.addTextChangedListener(new TextWatcher() {

    @Override
    public void onTextChanged(CharSequence s, int start, int before,
            int count) {
    }

    @Override
    public void beforeTextChanged(CharSequence s, int start, int count,
            int after) {
    }

    @Override
    public void afterTextChanged(Editable s) {

        // Check if edittext is empty
        if (TextUtils.isEmpty(s)) {
            // Disable ok button
            ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);

        } else {
            // Something into edit text. Enable the button.
            ((AlertDialog) dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);
        }

    }
});

下面是经过编辑的历史记录,可以作为更多细节参考

Here is a sample code, try this

AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle("Alert dialog title");
builder.setMessage("Dialog message");
builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        //DO TASK
    }
});
builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface arg0, int arg1) {
        //DO TASK
    }
});

AlertDialog dialog = builder.create();
dialog.show();

// After calling show method, you need to check your condition and enable/disable the dialog buttons 
if (your_condition_true) {
    // BUTTON1 is the positive button
    dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);
}

For negative button

dialog.getButton(AlertDialog.BUTTON2).setEnabled(false); //BUTTON2 is negative button

For buttons id:Reference alert_dialog.xml

Edited :

And the 100 since level 8 API (FroYo), does the same,

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

Edited

new AlertDialog.Builder(this)
    .setMessage("This may take a while")
    .setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {

        @Override
        public void onClick(DialogInterface dialog, int which) {
            ((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);
            // the rest of your stuff
        }

    }).show();

Android相关问答推荐

Android Jetpack编写如何在简单单击后显示工具提示

在Android中点击滑动时触发的手势

ENV变量在gradle进程中没有更新

Android事件:APP_SCOUT_HANG警告SQLite

我无法在底部导航栏中正确导航-Android底部导航视图

Android 12+BLE字节不同

Android Kotlin - 计费 - 从应用内购买获取productId

Android Studio SQLite 错误:列不正确(没有这样的列:_id)

在 Bash 脚本中使用 XMLLINT 解析 XML 单元测试文件,并将其放入数组中以表示成功和失败

Play store 的 Play 完整性与 Firebase 应用判断 Play 完整性

如何迭代 SqlDelight Select 结果而不将所有内容加载到内存中?

expo 上的 socket.io 无法从 Android 连接

更改当前活动并返回后,Android webview 滚动不起作用

错误:无法安装应用程序:INSTALL_PARSE_FAILED_MANIFEST_MALFORMED (React-Native-Android-Studio)

如何仅使用您的 Android 应用程序定位平板电脑?

DatePickerDialog (Android/Kotlin) 的两个问题

如果 auth 失败,如何显示 toast jetpack compose firebase if else @Composable 调用只能在 @Composable 函数的上下文中发生

喷气背包组成影子奇怪的行为

Android 自动分页如何与 media3 配合使用?

在 Android Studio 中替换字符串中的 "