Android: DatePickerDialog 和 TimePickerDialog 未通过选择

Android: DatePickerDialog and TimePickerDialog not passing selection

DatePickerDialog 和 TimePickerDialog 都有问题。用户可以使用加号或减号按钮 select 日期和时间。这行得通。但是当用户点击进入该字段时,他可以通过键盘设置一个值:

但这根本行不通。当用户键入新值并单击 "OK" 按钮时,当前 selected 字段的值 不会 传递给 onDateSet 方法。这在 DatePickerDialogTimePickerDialog 中都不起作用。相反,用户需要单击键盘上的绿色前进按钮才能真正设置 DatePickerDialog 中的值。当他按下 "OK" 时,值被传递,但这是一个非常愚蠢的可用性。没有人会这样做。这就是我实现片段的方式:

public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener
{
    private final DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd");

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        // Use the current date as the default date in the picker
        final Calendar c = Calendar.getInstance();
        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);
    }

    @Override
    public void onDateSet(DatePicker view, int year, int month, int day)
    {
        final Calendar calendar = Calendar.getInstance(Locale.getDefault());
        calendar.set(year, month, day);
        String pickedDate = dateFormat.format(calendar.getTime());

        ((MenuEditActivity)getActivity()).setMenuDate(pickedDate);
    }

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

有没有人遇到同样的问题并且已经实施了一些快速解决方法? (我不想重新实现整个选择器对话框,只是这个半生不熟的实现)。

我找到了 DatePickerDialog 的解决方法来禁用键盘,因此用户无法通过键盘输入日期。 (另见 [Disable keyboard input on Android TimePicker

   public class MyDatePickerDialog extends DatePickerDialog
    {
        public MyDatePickerDialog(Context context, OnDateSetListener listener, int year, int month, int dayOfMonth)
        {
            super(context, listener, year, month, dayOfMonth);
        }

        @NonNull
        @Override
        public DatePicker getDatePicker()
        {
            return super.getDatePicker();
        }
    }

片段中....

final MyDatePickerDialog datePickerDialog = new MyDatePickerDialog(getActivity(), this, year, month, day);
datePickerDialog.getDatePicker().setDescendantFocusability(DatePicker.FOCUS_BLOCK_DESCENDANTS);

但这对于 TimePickerDialog 是不可能的,因为没有方法可以获取 TimePicker,所以我使用了另一种时间选择器的解决方法,感谢 Mike M.

public class TimePickerFragment extends DialogFragment implements TimePickerDialog.OnTimeSetListener
{
    private TimePickerDialog timePickerDialog;

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState)
    {
        //Default 6:00
        timePickerDialog = new TimePickerDialog(getActivity(), this, 6, 0, android.text.format.DateFormat.is24HourFormat(getActivity()));

        return timePickerDialog;
    }

    @Override
    public void onStart()
    {
        super.onStart();

        final View hourView = timePickerDialog.findViewById(Resources.getSystem().getIdentifier("hour", "id", "android"));
        final View minuteView = timePickerDialog.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));

        if (hourView != null && hourView instanceof NumberPicker)
        {
            ((NumberPicker) hourView).setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        }

        if (minuteView != null && minuteView instanceof NumberPicker)
        {
            ((NumberPicker) minuteView).setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
        }
    }

    @Override
    public void onTimeSet(TimePicker view, int hourOfDay, int minute)
    {
        ((MenuEditActivity) getActivity()).setRecipeTime(hourOfDay, minute);
    }

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

这对我有用:

@SuppressWarnings("deprecation")
@Override
public void onTimeSet(TimePicker timePicker, int selectedHour, int selectedMinute) {
    timePicker.requestFocus();
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
        hour = timePicker.getHour();
        minute = timePicker.getMinute();
    } else {
        hour = timePicker.getCurrentHour();
        minute = timePicker.getCurrentMinute();
    }
}