如何禁用/启用对话框负正按钮? [英] How to disable / enable dialog negative positive buttons?

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

问题描述

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

 案例 DIALOG_TEXT_ENTRY://此示例显示如何向 AlertDialog 添加自定义布局LayoutInflater factory = LayoutInflater.from(this);最终视图 textEntryView = factory.inflate(R.layout.alert_dialog_text_entry, null);返回新的 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) {/* 用户点击 OK 所以做一些事情 */}}).setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {public void onClick(DialogInterface dialog, int whichButton) {/* 用户点击取消所以做一些事情 */}}).创建();}

解决方案

编辑以获得完整的解决方案...

AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);builder.setIcon(android.R.drawable.ic_dialog_info);builder.setTitle("提示对话框标题");builder.setMessage("这是如果附加到对话框的编辑文本为空时禁用按钮的示例代码片段.");builder.setPositiveButton("PositiveButton",新 DialogInterface.OnClickListener() {公共无效onClick(DialogInterface arg0,int arg1){//做任务}});builder.setNegativeButton("NegativeButton",新 DialogInterface.OnClickListener() {公共无效onClick(DialogInterface arg0,int arg1){//做任务}});//将 `EditText` 设置为 `dialog`.你也可以从 `xml` 添加 `EditText`.最终 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(输入);最终的 AlertDialog 对话框 = builder.create();对话框显示();//最初禁用按钮((AlertDialog) 对话框).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);//或者您可以在此处使用 setOnShowListener 首次禁用按钮.//现在为edittext设置textchange监听器input.addTextChangedListener(new TextWatcher() {@覆盖public void onTextChanged(CharSequence s, int start, int before,整数) {}@覆盖public void beforeTextChanged(CharSequence s, int start, int count,输入后) {}@覆盖公共无效 afterTextChanged(可编辑的){//检查edittext是否为空如果(TextUtils.isEmpty(s)){//禁用确定按钮((AlertDialog) 对话框).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);} 别的 {//编辑文本中的内容.启用按钮.((AlertDialog) 对话框).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(true);}}});

<小时><块引用>

以下是编辑过的历史,可以参考更多细节

这是一个示例代码,试试这个

AlertDialog.Builder builder = new AlertDialog.Builder(AddSchedule.this);builder.setIcon(android.R.drawable.ic_dialog_info);builder.setTitle("提示对话框标题");builder.setMessage("对话消息");builder.setPositiveButton("Button1", new DialogInterface.OnClickListener() {公共无效onClick(DialogInterface arg0,int arg1){//做任务}});builder.setNegativeButton("Button2", new DialogInterface.OnClickListener() {公共无效onClick(DialogInterface arg0,int arg1){//做任务}});AlertDialog 对话框 = builder.create();对话框显示();//调用 show 方法后,您需要检查您的条件并启用/禁用对话框按钮如果(your_condition_true){//BUTTON1 是正按钮dialog.getButton(AlertDialog.BUTTON1).setEnabled(false);}

对于否定按钮

dialog.getButton(AlertDialog.BUTTON2).setEnabled(false);//BUTTON2 为负按钮

对于按钮 id:参考 alert_dialog.xml

还有 setOnShowListener 自第 8 级 API (FroYo) 起,同样如此,

AlertDialog.Builder builder = new AlertDialog.Builder(this);builder.setPositiveButton(android.R.string.ok, null);AlertDialog 对话框 = builder.create();dialog.setOnShowListener(new OnShowListener() {@覆盖public void onShow(DialogInterface dialog) {如果(条件){((AlertDialog)dialog).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(false);}}});对话框显示();

<小时>

已编辑

new AlertDialog.Builder(this).setMessage("这可能需要一段时间").setPositiveButton("OK", new android.content.DialogInterface.OnClickListener() {@覆盖public void onClick(DialogInterface dialog, int which) {((AlertDialog)dialog).getButton(which).setVisibility(View.INVISIBLE);//剩下的东西}}).展示();

<小时>

Please look at the custom dialog below. I have an edittext field on the dialog and if the text field is empty I would like to disable the positiveButton. I can get a charListener for the text field but I am not sure how I am going to set the positivebutton to disable or enable from that listener? What is the reference for the positive and negative buttons?

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

    }
});


Below are edited history, which can be refer as some more details

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


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

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