Android AlertDialog onDismiss 获取用户选择
Android AlertDialog onDismiss get user choice
考虑以下 AlertDialog:
pizzaOrderDialog = new AlertDialog.Builder(mContext);
pizzaOrderDialog.setTitle("One last thing:");
pizzaOrderDialog.setMessage("Would you like black or green olives?")
.setCancelable(true)
.setPositiveButton("Black", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
orderPizza("black");
Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Green", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
orderPizza("green");
Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
}
});
alertD = pizzaOrderDialog.create();
用户也可能选择通过触摸屏幕上的任意位置来关闭对话框,而不是单击 "Black" 或 "Green"。我想要的是 "catch" 这种情况,并在 onDismiss 中得到这种情况的指示:
alertD.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//TODO tell if dialog result was dialogInterface.BUTTON_NEGATIVE
// or maybe dialogInterface.BUTTON_POSITIVE ?
}
});
此处的最佳做法是什么?谢谢!
显然,正如@CommonsWare 所指出的,我需要在这里使用 setOnCancelListener
。
考虑以下 AlertDialog:
pizzaOrderDialog = new AlertDialog.Builder(mContext);
pizzaOrderDialog.setTitle("One last thing:");
pizzaOrderDialog.setMessage("Would you like black or green olives?")
.setCancelable(true)
.setPositiveButton("Black", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
orderPizza("black");
Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
}
})
.setNegativeButton("Green", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
orderPizza("green");
Toast.makeText(mContext, "Ok! Thanks!", Toast.LENGTH_LONG).show();
}
});
alertD = pizzaOrderDialog.create();
用户也可能选择通过触摸屏幕上的任意位置来关闭对话框,而不是单击 "Black" 或 "Green"。我想要的是 "catch" 这种情况,并在 onDismiss 中得到这种情况的指示:
alertD.setOnDismissListener(new DialogInterface.OnDismissListener() {
@Override
public void onDismiss(DialogInterface dialogInterface) {
//TODO tell if dialog result was dialogInterface.BUTTON_NEGATIVE
// or maybe dialogInterface.BUTTON_POSITIVE ?
}
});
此处的最佳做法是什么?谢谢!
显然,正如@CommonsWare 所指出的,我需要在这里使用 setOnCancelListener
。