我正在使用Android 7.0(Nougat)
我创建EditText来显示时间,当点击它时,它将显示TimePicker,并在EditText中更新时间
一切顺利,但现在我要显示一个间隔5分钟的Timepicker
这意味着用户只能选择5,10,15,20 ...分钟
这是我的代码:
public class TimePickerFragment extends DialogFragment
implements TimePickerDialog.OnTimeSetListener {
private EditText mEditText;
private int initHour;
private int initMinute;
public void setEditText(EditText editText) {
mEditText = editText;
}
public void setInitHour(int hour) {
initHour = hour;
}
public void setInitMinute(int minute) {
initMinute = minute;
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
return new TimePickerDialog(getActivity(), this, initHour, initMinute, DateFormat.is24HourFormat(getActivity()));
}
@Override
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
SimpleDateFormat timeSdf = new SimpleDateFormat("HH:mm", Locale.US);
Calendar calendar = Calendar.getInstance();
calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);
calendar.set(Calendar.MINUTE, minute);
mEditText.setText(timeSdf.format(calendar.getTime()));
}
}
在活动中:
TimePickerFragment dialogFragment = new TimePickerFragment();
dialogFragment.setInitHour(11);
dialogFragment.setInitMinute(30);
dialogFragment.setEditText(mStartTimeText);
dialogFragment.show(getFragmentManager(), "startTimePicker");
我研究了相关的帖子,但所有的解决方案都不是最新的 我找不到适合Nougat版本的解决方案 如果对我有任何暗示,我将非常感激!谢谢。