2

DatePickerDialog和TimePickerDialog都存在问题。用户可以使用加号或减号按钮选择日期和时间。这工作。但是,当用户点击进入该领域,他可以通过键盘设定值:Android:DatePickerDialog和TimePickerDialog未通过选择

picker dialog

但是,这并不在所有的工作。当用户键入一个新值并点击“确定”按钮时,当前选择的字段的值不会被传递给onDateSet方法。这既不在DatePickerDialog也不在TimePickerDialog。相反,用户需要点击键盘上的绿色前进按钮才能真正设置DatePickerDialog中的值。当他按下“确定”时,该值就通过了,但这是一个非常愚蠢的可用性。没有人会这样做。这是我如何实现的片段:

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对话框,仅仅是这个不完整的实现)。

+0

嗯,我没有观察到你描述的行为,但是如果你想用'TimePickerDialog'尝试可聚焦技巧,你应该可以使用类似于[这个答案](http://stackoverflow.com/a/39071972)。你可以在'TimePickerDialog'上调用'findViewById()',而不是''ampm_layout''使用''timePicker'',返回的'View'应该是'TimePicker'。 (显然,你会忽略可见性的东西。)你可能需要在'Fragment'的'onStart()'方法中做到这一点,以确保所有东西都已经被正确初始化。 –

+0

@MikeM。谢谢,它现在可以工作(请参阅我更新的问题)。如果你把它作为答案发布,我可以接受它。顺便说一句,这很奇怪。我昨天已经尝试了类似的东西,但我总是得到空。也许是因为我没有在'onStart()'中做。 – Bevor

+0

没问题。是的,我有机会更早地进行快速测试,并且它在'onCreateDialog()'中表现不佳。我没有真正调查原因,但它在'onStart()'中工作得很好。 Anyhoo,你丰富的解决方案比我的小建议要好得多。您应该将这些编辑转换为答案。不过谢谢你。欣赏此优惠。干杯! –

回答

2

我找到了解决方法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,所以我用另一种解决方法的时间选择器,这要归功于迈克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); 
    } 
} 
+0

不错,彻底。当你可以的时候不要忘记接受。干杯! –

+0

@MikeM。 SO迫使我等待一天,直到我能接受它。 – Bevor

+0

是的,我知道这是一两天。虽然不确定。 –

0

这为我工作:

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