How to disable / enable dialog negative positive b

2020-01-27 01:24发布

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

7条回答
▲ chillily
2楼-- · 2020-01-27 01:48

For delete record from database list view using view holder you used this code in your getview() method..

viewHolder.btn.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View arg0) {
                    // TODO Auto-generated method stub
                    AlertDialog.Builder alertDialog2 = new AlertDialog.Builder(
                            Favorate.this.getParent());

                    // Setting Dialog Title
                    alertDialog2.setTitle("Confirm Delete...");

                    // Setting Dialog Message
                    alertDialog2
                            .setMessage("Are you sure you want delete ?");

                    // Setting Icon to Dialog
                    alertDialog2.setIcon(R.drawable.delete);

                    // Setting Positive "Yes" Btn
                    alertDialog2.setPositiveButton("YES",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // Write your code here to execute after
                                    // dialog

                                    int id = _items.get(position).id;
                                    db.deleterecord(id);

                                    db.close();
                                }
                            });
                    // Setting Negative "NO" Btn
                    alertDialog2.setNegativeButton("NO",
                            new DialogInterface.OnClickListener() {
                                public void onClick(DialogInterface dialog,
                                        int which) {
                                    // Write your code here to execute after
                                    // dialog

                                    dialog.cancel();
                                }
                            });

                    // Showing Alert Dialog
                    alertDialog2.show();

                }
            });

Read More

查看更多
登录 后发表回答