[Android]Alertdialog return boolean onclick
I have a problem with a AlertDialog : I'ld like that my AlertDialog return true if the user click on Positive button and false if he click on negative button. The function must block the program until the user click on a button.
My code :
public static boolean errorMD5(Context context){
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setTitle(context.getString(R.string.error));
builder.setIcon(android.R.drawable.ic_dialog_alert);
builder.setMessage(R.string.errorMD5);
builder.setPositiveButton(R.string.retry, new DialogInterface.OnClickListener() {
public void onClick(DialogInte开发者_运维知识库rface dialog, int which) {
//The function return true
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface arg0, int arg1) {
//The function return false
}
});
AlertDialog alert = builder.create();
alert.show();
}
In your Activity you should implement 2 functions.
onYesPressed() and onNoPressed()
and call them from your dialog.
Or you can implement just a function that takes a boolean parameter
onUserDismissDialog(boolean allow)
and call this function from your dialog OnClickListener.
精彩评论