Android 从其中关闭AlertDialog';s按钮';s回调

Android 从其中关闭AlertDialog';s按钮';s回调,android,Android,我希望能够在自己的按钮回调中动态关闭AlertDialog: final AlertDialog.Builder alert = new AlertDialog.Builder(this); alert.setNeutralButton(R.string.enter, new DialogInterface.OnClickListener() { public void onClick(DialogInterface dialog, int whichButton

我希望能够在自己的按钮回调中动态关闭
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\u对话框
上调用
dismise()
,但我不能将此调用放在创建它的代码中

回调中的
对话框接口
对话框
本身(实现
对话框接口
),因此您只需调用该方法:


回调中的
对话框接口
对话框
本身(实现
对话框接口
),因此您只需调用该方法:

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
        }
    }
});