[英]Dismiss AlertDialog from within it's button's callback
我希望能够AlertDialog
自己的按钮回调中动态消除AlertDialog
:
final AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (/* if some condition is met */) {
// dismiss the alert
} else {
// keep the alert open
}
}
});
final AlertDialog alert_dialog = alert.create();
alert_dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
alert_dialog.setCanceledOnTouchOutside(false);
alert_dialog.show();
我知道我可以在alert_dialog
上调用dismiss()
,但是不能将其放在创建它的代码中。
回调中的DialogInterface
是Dialog
本身( Dialog
实现DialogInterface
),因此您所要做的就是调用DialogInterface#dismiss()
方法:
alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
if (/* if some condition is met */) {
dialog.dismiss(); // dismiss the alert
} else {
// keep the alert open
}
}
});
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.