How to get the response from a modal dialog?
I currently开发者_Go百科 have this:
Builder yesandno = new AlertDialog.Builder(this);
yesandno.setTitle("QuickResponse");
yesandno.setMessage(message);
yesandno.setPositiveButton("YES", null);
yesandno.setNegativeButton("NO", null);
yesandno.show();
How should I go by setting an event listener that will capture if the user clicked YES or NO?
When you call setPositiveButton()
and setNegativeButton()
instead of passing in null
you should pass in a DialogInterface.OnClickListener
.
For example:
yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//User clicked yes!
}
});
Just do something like:
yesandno.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked yes
}
});
yesandno.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User clicked no
}
});
and do whatever you want in the button callbacks.
精彩评论