如何处理关闭RadioButtonDialog

时间:2018-08-30 09:56:43

标签: android radio-button alertdialog

我有这个RadioPickerDialog:

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    LayoutInflater inflater = getActivity().getLayoutInflater();
    View view = inflater.inflate(R.layout.dialog_radio_buttons, null);
    ButterKnife.bind(this, view);
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    TextView tvTitle = view.findViewById(R.id.dialog_radio_title);
    tvTitle.setText(title);
    builder.setView(view)
            .setPositiveButton(R.string.btn_ok, (DialogInterface.OnClickListener) (dialog, which) -> {
                listener.onPickerConfirmClicked(getSelectedIndex());
                dialog.dismiss();
            })
            .setNegativeButton(R.string.btn_cancel, (DialogInterface.OnClickListener) (dialog, which) -> {
                dialog.dismiss();
            });

    for (int i = 0; i < items.size(); i++) {
        RadioButton radioButton = new RadioButton(getActivity());
        T item = items.get(i);
        radioButton.setText(item.toString());
        radioGroup.addView(radioButton);
    }

    RadioButton buttonCheckedByDefault = (RadioButton) radioGroup.getChildAt(defItemNum);
    buttonCheckedByDefault.setChecked(true);
    return builder.create();
}

对话框调用:

UiUtils.showRadioButtonPickerDialog(this, reasonList, getString(R.string.workshift_pause), 0, new RadioButtonPickerDialog.PickerListener() {
                @Override
                public void onPickerConfirmClicked(int selectedIndex) {
                    presenter.setPause(reasonList.get(selectedIndex), true);
                }
            }
    );

我要处理未单击事件(在非对话框中单击或单击取消按钮)。我该怎么办?

2 个答案:

答案 0 :(得分:1)

我找到了解决方法:

....
//When clicked negativeButton
@Override
public void onCancel(DialogInterface dialog){
    dialog.dismiss();
}

//when clicked not in the dialog box
@Override
public void onDismiss(DialogInterface dialog){
    dialog.dismiss();
}

答案 1 :(得分:0)

只需将监听器添加到关闭事件即可,例如通过setOnDismissListener()方法。最好在此方法内调用某种外部类级别的回调,以将工作委托给调用者(片段/活动,显示此对话框),这样您将始终引用当前显示的片段,万一您可能需要显示多个。