如何禁用/启用对话框负正面按钮?对话框、按钮

2023-09-12 09:07:47 作者:叶落若相随

请看看下面的自定义对话框。我有一个EditText字段对话框中,如果文本字段为空,我想禁用 positiveButton 。我可以得到一个charListener文本字段,但我不知道我会如何设置 positivebutton 禁用或从听者能?什么是对正和负的按钮参考

 案例DIALOG_TEXT_ENTRY:
    //这个例子说明如何自定义布局添加到AlertDialog
    LayoutInflater厂= LayoutInflater.from(本);
    最后查看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,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释whichButton){
                / *用户点击OK等做一些东西* /
            }
        })
        .setNegativeButton(R.string.alert_dialog_cancel,新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface对话,诠释whichButton){
                / *用户点击取消,以便做一些东西* /
            }
        })
        。创建();
}
 

解决方案

下面是一个简单的code,试试这个的

  AlertDialog.Builder建设者=新AlertDialog.Builder(AddSchedule.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle(警告对话框的标题);
builder.setMessage(对话消息);
builder.setPositiveButton(Button1的,新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface为arg0,INT ARG1){
        //完成任务
    }
});
builder.setNegativeButton(Button2的新DialogInterface.OnClickListener(){
    公共无效的onClick(DialogInterface为arg0,INT ARG1){
        //完成任务
    }
});

AlertDialog对话框= builder.create();
dialog.show();

//调用显示方法后,您需要检查你的病情和
//使对话框/关闭按钮
如果(your_condition_true)
    dialog.getButton(AlertDialog.BUTTON1).setEnabled(假); // BUTTON1是积极的按钮
 
MFC应用实例 禁用对话框右上角的关闭按钮

对于负按钮的

  dialog.getButton(AlertDialog.BUTTON2).setEnabled(假); // BUTTON2是负的按钮
 

对于按钮ID :参考的alert_dialog.xml

编辑:

和setOnShowListener自8级API(升级Froyo),做同样的,的

  AlertDialog.Builder建设者=新AlertDialog.Builder(本);
builder.setPositiveButton(android.R.string.ok,NULL);
AlertDialog对话框= builder.create();
dialog.setOnShowListener(新OnShowListener(){

    @覆盖
    公共无效OnShow中(DialogInterface对话){
        如果(条件)
        ((AlertDialog)对话框).getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(假);
    }
});

dialog.show();
 

编辑

 新AlertDialog.Builder(本)
    .setMessage(这可能需要一段时间)
    .setPositiveButton(OK,新android.content.DialogInterface.OnClickListener(){
    @覆盖
    公共无效的onClick(DialogInterface对话,诠释它){
         ((AlertDialog)对话框).getButton(所).setVisibility(View.INVISIBLE);
         //你的东西休息
    }
})
    。显示();
 

编辑为完整的解决方案... 的

  AlertDialog.Builder建设者=新AlertDialog.Builder(MainActivity.this);
builder.setIcon(android.R.drawable.ic_dialog_info);
builder.setTitle(警告对话框的标题);
builder.setMessage(这是例如code段禁用按钮,如果的EditText连接到对话框是空的。);
builder.setPositiveButton(PositiveButton
        新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface为arg0,INT ARG1){
                //完成任务
            }
        });
builder.setNegativeButton(NegativeButton
        新DialogInterface.OnClickListener(){
            公共无效的onClick(DialogInterface为arg0,INT ARG1){
                //完成任务
            }
        });
//设置`EditText`为`dialog`。您可以添加`EditText`从`xml`了​​。
最后的EditText输入=新的EditText(MainActivity.this);
LinearLayout.LayoutParams LP =新LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.MATCH_PARENT);
input.setLayoutParams(LP);
builder.setView(输入);
最后AlertDialog对话框= builder.create();
dialog.show();
//开始禁用按钮
((AlertDialog)对话框).getButton(AlertDialog.BUTTON_POSITIVE)
        .setEnabled(假);
//也可以使用这里setOnShowListener首先禁用按钮
// 时间。

//现在设置textchange侦听器的EditText
input.addTextChangedListener(新TextWatcher(){
    @覆盖
    公共无效onTextChanged(CharSequence中,诠释开始,诠释之前,
            诠释计数){
    }

    @覆盖
    公共无效beforeTextChanged(CharSequence中,诠释开始,诠释计数,
            之后INT){
    }

    @覆盖
    公共无效afterTextChanged(编辑S){
        //检查的EditText为空
        如果(TextUtils.isEmpty(多个)){
            //禁用OK按钮
            ((AlertDialog)对话框).getButton(
                    AlertDialog.BUTTON_POSITIVE).setEnabled(假);
        } 其他 {
            //东西放到编辑文本。启用按钮。
            ((AlertDialog)对话框).getButton(
                    AlertDialog.BUTTON_POSITIVE).setEnabled(真正的);
        }

    }
});
 

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

解决方案

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

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

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

    }
});