AlertDialog 点击按钮后不关闭对话框

AlertDialog 点击按钮后不关闭对话框_第1张图片
Button 点击后自动关闭

这里的问题:当我点击确定按钮,也就是 AlertDialog 里的 PositiveButton 的时候,
我们需要判断用户是输入是否符合我们的预期,
如果不符合通常提示用户重写输入,且不关闭当前的对话框,
但上图中点击按钮后会自动的关闭窗口。

关于 AlertDialog 的用法见于我的 Dialog 笔记
先看原来的这个是怎么写的:

private void openDialog() {
    LinearLayout linearLayout = (LinearLayout) LayoutInflater.from(getContext()).inflate(R.layout.change_password_dialog, null);
    final EditText originPasswordEt = (EditText) linearLayout.findViewById(R.id.origin_password);
    TextView forgetPassword = (TextView) linearLayout.findViewById(R.id.forget_password);
   
    final AlertDialog dialog = new AlertDialog.Builder(getContext())
        .setView(linearLayout)
        .setPositiveButton("确定", new DialogInterface.OnClickListener() {
            @Override
                public void onClick(DialogInterface dialog, int which) {
                String originPassword = originPasswordEt.getText().toString().trim();
                    //传到后台
                }
        })
        .create();

    dialog.show();
}

虽然图片里和代码的并不是同一个,但问题是一样的
setPositiveButton 方法中,即使我们没有调用 dialog.dismiss()
但对话框还是会自动的关闭,就算我们在 onClick 里判断输入的内容,错误的提示也会在窗口关闭后才出现。

在 AlertDialog 提供的 API 中我也没有找到可以设置的地方,如果有还请告知。
解决这个问题的办法可以直接获取到 Btn 的实例去设置事件:

final AlertDialog dialog = new AlertDialog.Builder(getActivity())
    .setTitle(msg)
    .setView(layout)
    .setNegativeButton("取消", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    })
    .setPositiveButton("submit",null)
    .setCancelable(true)
    .create();
dialog.show();

//为了防止 getButton() 为空,需要在 OnShowListener 里设置事件
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
    @Override
    public void onShow(DialogInterface dialog) {
        //为了避免点击 positive 按钮后直接关闭 dialog,把点击事件拿出来设置
        dialog.getButton(AlertDialog.BUTTON_POSITIVE)
          .setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Pattern pattern = Pattern.compile("[0-9]*");
                Matcher matcher = pattern.matcher(editText.getText());
                if (!matcher.matches()){
                    showToast("请输入正确的 ID");
                    break;
                }
                dialog.dismiss();
            }
        });
    }
});
  • setPositiveButton("submit",null) 监听事件传入 null
  • 在调用 dialog.show() 后再设置 Button 的点击事件,否则 getButton() 会返回空

这样在我们手动调用 dialog.dismiss() 之前,对话框是不会关闭的。

以下是 Dialog(support.v7) 的源码

首先是 AlertDialog 的 setPositiveButton() 为 AlertController.mPositiveButtonListener 赋值

public Builder setPositiveButton(@StringRes int textId, final OnClickListener listener) {
    P.mPositiveButtonText = P.mContext.getText(textId);
    P.mPositiveButtonListener = listener;
    return this;
}

AlertDialog.create() 中调用 AlertController.apply()

public void apply(AlertController dialog) {
    // ...
    if (mPositiveButtonText != null) {
        dialog.setButton(DialogInterface.BUTTON_POSITIVE, mPositiveButtonText,
                mPositiveButtonListener, null);
    }
    // ...
}

dialog.setButton() 中为 mButtonPositiveMessage 赋值

public void setButton(int whichButton, CharSequence text,
        DialogInterface.OnClickListener listener, Message msg) {
    if (msg == null && listener != null) {
        msg = mHandler.obtainMessage(whichButton, listener);
    }
    // mButtonPositiveMessage.what = DialogInterface.BUTTON_POSITIVE
    // mButtonPositiveMessage.obj = mPositiveButtonListener
    switch (whichButton)  {
        case DialogInterface.BUTTON_POSITIVE:
            mButtonPositiveText = text;
            mButtonPositiveMessage = msg;
            break;
    // ...
}

AlertController 使用 mButtonHandler 为所有的 Button 中转 click 事件

private final View.OnClickListener mButtonHandler = new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        final Message m;
        if (v == mButtonPositive && mButtonPositiveMessage != null) {
            m = Message.obtain(mButtonPositiveMessage);
        } else if (v == mButtonNegative && mButtonNegativeMessage != null) {
            m = Message.obtain(mButtonNegativeMessage);
        } else if (v == mButtonNeutral && mButtonNeutralMessage != null) {
            m = Message.obtain(mButtonNeutralMessage);
        } else {
            m = null;
        }

        if (m != null) {
            m.sendToTarget();
        }

            // Post a message so we dismiss after the above handlers are executed
        mHandler.obtainMessage(ButtonHandler.MSG_DISMISS_DIALOG, mDialog)
        .sendToTarget();
    }
};

最后在 ButtonHandler 中的 handleMessage() 处理 Message 事件

@Override
public void handleMessage(Message msg) {
    switch (msg.what) {
        case DialogInterface.BUTTON_POSITIVE:
        case DialogInterface.BUTTON_NEGATIVE:
        case DialogInterface.BUTTON_NEUTRAL:
            ((DialogInterface.OnClickListener) msg.obj).onClick(mDialog.get(), msg.what);
            break;
        case MSG_DISMISS_DIALOG:
            ((DialogInterface) msg.obj).dismiss();
    }
}

本文提到的问题就是所有的 click 事件最后都会 send ButtonHandler.MSG_DISMISS_DIALOG 决定了 Dialog 会被 dismiss,所以上面的业务不适用 setPositiveButton() 设置 click 事件。

写完发现乱的不行...本来就是很简单的东西就这样吧

你可能感兴趣的:(AlertDialog 点击按钮后不关闭对话框)