为 MaterialDatePicker 扩展 DialogFragment

Extending DialogFragment for a MaterialDatePicker

我想从 DatePickerDialog 迁移到 MaterialDatePicker。 我 运行 从 MaterialDatePicker 调用 getDialog 时遇到问题,dialog 结果始终为 null .

这是一个错误,还是 MaterialDatePicker 不支持该功能?

DatePickerDialog

private static class BirthDateUpdater extends DialogFragment implements DatePickerDialog.OnDateSetListener {

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        // create calendar instance
        final Calendar c = Calendar.getInstance();

        // set calendar to birthdate
        c.setTimeInMillis(getBirthDate().getTime());

        int year = c.get(Calendar.YEAR);
        int month = c.get(Calendar.MONTH);
        int day = c.get(Calendar.DAY_OF_MONTH);

        // Create a new instance of DatePickerDialog and return it
        return new DatePickerDialog(getActivity(), this, year, month, day);
    }

    public void onDateSet(DatePicker view, int year, int month, int day) {
        // process date selected in here
    }
}

MaterialDatePicker 问题 - picker.getDialog() 的结果总是 null?

public static class BirthDateUpdater extends DialogFragment implements 
    MaterialPickerOnPositiveButtonClickListener<Long>, 
    DialogInterface.OnCancelListener {
    
    private Dialog dialog;

    public BirthDateUpdater() {
    }

    @NonNull
    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        try {
            // builder
            MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
            builder.setTitleText("Select Date");

            // set date to BirthDate
            builder.setSelection(getBirthDate().getTime());

            // picker
            MaterialDatePicker<Long> picker = builder.build();
            
            // add event
            picker.addOnPositiveButtonClickListener(this);
            picker.addOnCancelListener(this);

            // dialog
            dialog = picker.getDialog(); // error in here (dialog is null)?

        } catch (Exception ex) {
            ex.printStackTrace();
        }
        return dialog;
    }

    @Override
    public void onPositiveButtonClick(Long selection) {
        try {
            Calendar dateSelected = Calendar.getInstance();
            dateSelected.setTimeInMillis(selection);
            
            // process date selected in here
            
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }

    @Override
    public void onCancel(@NonNull DialogInterface dialog) {
        dialog.dismiss();
    }
}

MaterialDatePicker 已经是 DialogFragment。在您的情况下 picker.getDialog() returns null 因为在后台 DialogFragment 将值写入 DialogFragment.mDialog(此字段的值 returns picker.getDialog())从生命周期回调中提交onCreateDialog() 将在调用对话框的 show 方法后调用异步,即当您调用 picker.show(fragmentManager) 然后在选择器的 onCreateDialog 中,您将无法从 getDialog() 方法获得非空值。

在你的情况下,你不应该扩展 DialogFragment 或 MaterialDatePicker(它是 final ),而是你可以创建像 birthDateUpdater:

这样的方法
public DialogFragment birthDateUpdater() {
            MaterialDatePicker.Builder<Long> builder = MaterialDatePicker.Builder.datePicker();
            builder.setTitleText("Select Date");

            // set date to BirthDate
            builder.setSelection(getBirthDate().getTime());

            // picker
            MaterialDatePicker<Long> picker = builder.build();
            
            // add event
            picker.addOnPositiveButtonClickListener(selection -> {
              try {
              Calendar dateSelected = Calendar.getInstance();
              dateSelected.setTimeInMillis(selection);
            
               // process date selected in here
            
               } catch (Exception ex) {
                 ex.printStackTrace();
               }
            });
            picker.addOnCancelListener(dialog -> {
               dialog.dismiss();
            });
            return picker
}

或者创建class,工厂,封装这个逻辑的任何东西。