【问题标题】:Android AlertDialog with DialogFragment: don't close the dialog even if OK is clicked带有DialogFragment的Android AlertDialog:即使单击确定也不要关闭对话框
【发布时间】:2013-01-27 15:58:34
【问题描述】:

我有一个带有自定义布局的 AlertDialog(只是一个 EditText),我想在单击 OK 按钮时验证数据。如果验证失败,我不想关闭对话框。

我正在使用对话框的默认按钮(正面和负面)。如果我使用 "setPositiveButton("", new DialogInterface.OnClickListener() ..." 对话框总是关闭。我看过几个帖子,他们说应该覆盖 onClick 侦听器,但我无法让它工作. 这是我找到的代码:

Button theButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
theButton.setOnClickListener(new CustomListener(dialog));

因为它说应该在显示对话框之后完成,所以我将此代码放在我的活动中,而不是在我的 DialogFragment 中,但如果我使用 mDialogFragment.getDialog(),它总是返回 null。

这是我的对话片段的一部分:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    // Use the Builder class for convenient dialog construction
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());

    builder.setTitle(R.string.new);

    LayoutInflater inflater = getActivity().getLayoutInflater();
    dialogView = inflater.inflate(R.layout.edit_license, null);

    builder.setView(dialogView)

    // Add action buttons
    .setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int id) {

        }
    })
    .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
        public void onClick(DialogInterface dialog, int id) {
            MyDialogFragment.this.getDialog().cancel();
        }
    }); 

    return builder.create();

}

在我的活动中,我执行以下操作:

DialogFragment dialog = new MyDialogFragment(true, null);
dialog.show(getSupportFragmentManager(), "EditLicenseDialogFragment");

AlertDialog alertDialog = (AlertDialog)dialog.getDialog();
alertDialog.getButton(DialogInterface.BUTTON_POSITIVE);
btnPositive.setOnClickListener(new CustomListener(alertDialog));

... 

class CustomListener implements View.OnClickListener {
    private final Dialog dialog;
    public CustomListener(Dialog dialog) {
        this.dialog = dialog;
    }
    @Override
    public void onClick(View v) {
        ...
    }
}

就像我说的,(AlertDialog)dialog.getDialog();总是返回 null。这是为什么?如果验证不正确,如何避免关闭对话框?

谢谢!

【问题讨论】:

    标签: android android-alertdialog android-dialogfragment


    【解决方案1】:

    这个解决方案对我有用。对话框已经在 onResume 中可见,因此这是访问按钮和替换侦听器的好地方。

    public class XYZFragment extends DialogFragment {
       ...
    
        @Override
        public void onResume() {
            super.onResume();
    
            AlertDialog dialog = (AlertDialog)getDialog();
    
            Button positiveButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
            positiveButton.setOnClickListener(new View.OnClickListener() {                  
                @Override
                public void onClick(View onClick) {
                ...
                }
            });
        }
    }
    

    【讨论】:

    • 我现在看不到确定按钮
    【解决方案2】:

    这里有一些来自DialogFragment参考的文字:

    显示对话窗口的片段,浮动在其顶部 活动的窗口。这个片段包含一个 Dialog 对象,它 根据片段的状态适当显示。 控制 对话框(决定何时显示、隐藏、关闭它)应该完成 通过此处的 API,而不是直接调用对话框。

    您应该调用 getDialog().dismiss() 而不是调用 dismiss()

    【讨论】:

    • 有趣。那么 getButton 函数呢?它不在 DialogFragment 上,如果我尝试获取 Dialog,它会返回 null。
    • 对话框返回 null 因为 getDialog 返回的 mDialog 尚未创建(还)。解决方案是在 DialogFragment 中实现其他生命周期方法,这些方法发生在 onCreateDialog/show 函数之后。但这并不是真正的问题,因为您只需在 dialogfragment 中定义 clickListener(就像现在一样)。但是不要在dialog上执行对话操作,而是在dialogfragment上执行,这样你就不需要调用getDialog()
    猜你喜欢
    • 1970-01-01
    • 2014-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-03-14
    • 2023-03-20
    • 1970-01-01
    • 2021-12-29
    • 1970-01-01
    相关资源
    最近更新 更多