Java – use dialoginterface Onclicklistener distinguishes a single dialog

We have two alertdialog objects

AlertDialog dialog1,dialog2;

Both dialog boxes are through alertdialog Created by builder How do we recognize dialoginterface Which dialog box in onclicklistener is the event source?

Using a single dialog box, we can do this:

AlertDialogInstance.setOnClickListener(myListener);

//myListener
public void onClick(DialogInterface arg0,int arg1) {
        switch (arg1) {
            case AlertDialog.BUTTON_NEGATIVE:
                // do something
                break;
            case AlertDialog.BUTTON_POSITIVE:
                // do something
                break;
            case AlertDialog.BUTTON_NEUTRAL:
                // do something
                break;
        }
    }

How can I modify this switch logic to handle multiple dialog boxes? (or, if there is a better system to handle dialog boxes, what is it except inline button callback?)

Solution

I suggest you put the param you need in the custom listener

private class CustomOnClickListener implements OnClickListener {
    private int id;

    public CustomOnClickListener(int id) {
       this.id = id;
    }

    public void onClick(DialogInterface dialog,int which) {
            //check id and which
        }

}

Then, when you add onclicklisteners to the dialog box, you simply provide the listener with an ID

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>