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对话框,只是这个半生不熟的实现)。
答案 0 :(得分: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
,所以我使用了另一种解决方法来解决时间问题,感谢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);
}
}
答案 1 :(得分: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();
}
}