Reputation: 1949
I got an AlertDialog that has one positive and negative button each. Within the onClick(DialogInterface dialog, int item) event handler, which is a member function of Activity, how do I tell which button has been clicked? Base on my observation, the "item" parameter has -1 as value if positive button is clicked, or -2 if it's negative button. However, I couldn't find any information on such from API doc hence I feel that this could be broken at any time.
Upvotes: 1
Views: 1712
Reputation: 5786
AlertDialog's positive and negative buttons use the DialogInterface.OnClickListener interface to respond to selection. Like you said, the onClick method of this callback looks like this:
public void onClick(DialogInterface dialog, int which) {
// ...
}
The which parameter indicates which button was clicked and can have, among others, the following values: 1) AlertDialog.BUTTON_POSITIVE (-1) 2) AlertDialog.BUTTON_NEGATIVE (-2)
So, you clicked the positive button if which is -1 and the negative if which is -2.
Upvotes: 4