【问题标题】:Android: DatePickerDialog and TimePickerDialog not passing selectionAndroid:DatePickerDialog 和 TimePickerDialog 未通过选择
【发布时间】:2023-03-27 01:29:01
【问题描述】:

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

但这根本不起作用。当用户输入新值并单击“确定”按钮时,当前选定字段的值不会传递给onDateSet 方法。这既不适用于DatePickerDialog,也不适用于TimePickerDialog。相反,用户需要单击键盘上的绿色前进按钮才能真正设置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);
    }
}

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

【问题讨论】:

  • 好吧,我没有观察到你描述的行为,但如果你想尝试使用 TimePickerDialog 的焦点技巧,你应该能够使用类似于的方法获得 TimePicker一个显示在this answer。你可以在你的TimePickerDialog 上调用findViewById(),而不是使用"timePicker" 代替"ampm_layout",返回的View 应该是TimePicker。 (显然,你会忽略可见性的东西。)你可能需要在 FragmentonStart() 方法中执行此操作,以确保首先正确初始化所有内容。
  • @MikeM。谢谢,它现在可以工作(请参阅我更新的问题)。如果您将其发布为答案,我可以接受。顺便说一句,这很奇怪。我昨天已经尝试过类似的东西,但我总是得到空值。也许是,因为我没有在onStart() 中这样做。
  • 没问题。是的,我之前有机会运行一个快速测试,但它在onCreateDialog() 中表现不佳。我并没有真正调查原因,但它在onStart() 中工作得很好。 Anyhoo,您充实的解决方案比我的小建议要好得多。您应该将这些编辑转移到答案中。不过,谢谢。欣赏报价。干杯!

标签: android android-datepicker android-timepicker datepickerdialog timepickerdialog


【解决方案1】:

我为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);
    }
}

【讨论】:

  • 很好,很彻底。不要忘记在可能的情况下接受这一点。干杯!
  • @MikeM。所以强迫我等一天,直到我能接受。
  • 是的,我知道那是一两天。不过不确定。
  • @MikeM.i 想在我的活动中使用此代码,但不知道如何使用它并且 setRecipeTime 未检测到..help
【解决方案2】:

这对我有用:

@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();
    }
}

【讨论】:

    猜你喜欢
    • 2015-09-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-06-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多