繁体   English   中英

从按钮的回调中关闭AlertDialog

[英]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() ,但是不能将其放在创建它的代码中。

回调中的DialogInterfaceDialog本身( 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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM