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

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

问题描述

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

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 buttons of dialog 
if(your_condition_true)
    dialog.getButton(AlertDialog.BUTTON1).setEnabled(false); //BUTTON1 is positive button

对于否定按钮

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

对于按钮ID 参考 alert_dialog.xml

已编辑

setOnShowListener ,因为level 8 API(FroYo),执行相同的

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






已编辑 p>


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天全站免登陆