Android TimePickerDialog设置最大时间

我正在寻找一种方法来设置在Android上可以选择的最大和最小时间TimePickerDialog,并将默认的分钟间隔从1分钟更改为5分钟,

我以为那是一件容易的事,但我找不到办法!


猛跑小猪
浏览 1596回答 3
3回答

郎朗坤

对于lolipop和更高版本的android,您可以使用此修改过的RangeTimePickerDialog类(从lolipop,Timepicker默认使用时钟模式(材料设计),因此旧的自定义类将无法使用。我们可以将模式更改为Spinner以获取最新版本,并可以重用该类)public class RangeTimePickerDialog extends TimePickerDialog {&nbsp; &nbsp; private int minHour = -1;&nbsp; &nbsp; private int minMinute = -1;&nbsp; &nbsp; private int maxHour = 25;&nbsp; &nbsp; private int maxMinute = 25;&nbsp; &nbsp; private int currentHour = 0;&nbsp; &nbsp; private int currentMinute = 0;&nbsp; &nbsp; private Calendar calendar = Calendar.getInstance();&nbsp; &nbsp; private DateFormat dateFormat;&nbsp; &nbsp; public RangeTimePickerDialog(Context context, int dialogTheme, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, callBack, hourOfDay, minute, is24HourView);&nbsp; &nbsp; &nbsp; &nbsp; currentHour = hourOfDay;&nbsp; &nbsp; &nbsp; &nbsp; currentMinute = minute;&nbsp; &nbsp; &nbsp; &nbsp; dateFormat = DateFormat.getTimeInstance(DateFormat.SHORT);&nbsp; &nbsp; &nbsp; &nbsp; fixSpinner(context, hourOfDay, minute, is24HourView);&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<?> superclass = getClass().getSuperclass();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field mTimePickerField = superclass.getDeclaredField("mTimePicker");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTimePickerField.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimePicker mTimePicker = (TimePicker) mTimePickerField.get(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; mTimePicker.setOnTimeChangedListener(this);&nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchFieldException e) {&nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalArgumentException e) {&nbsp; &nbsp; &nbsp; &nbsp; } catch (IllegalAccessException e) {&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; public void setMin(int hour, int minute) {&nbsp; &nbsp; &nbsp; &nbsp; minHour = hour;&nbsp; &nbsp; &nbsp; &nbsp; minMinute = minute;&nbsp; &nbsp; }&nbsp; &nbsp; public void setMax(int hour, int minute) {&nbsp; &nbsp; &nbsp; &nbsp; maxHour = hour;&nbsp; &nbsp; &nbsp; &nbsp; maxMinute = minute;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {&nbsp; &nbsp; &nbsp; &nbsp; boolean validTime = true;&nbsp; &nbsp; &nbsp; &nbsp; if (hourOfDay < minHour || (hourOfDay == minHour && minute < minMinute)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validTime = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (hourOfDay&nbsp; > maxHour || (hourOfDay == maxHour && minute > maxMinute)){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validTime = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if (validTime) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentHour = hourOfDay;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentMinute = minute;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; updateTime(currentHour, currentMinute);&nbsp; &nbsp; &nbsp; &nbsp; updateDialogTitle(view, currentHour, currentMinute);&nbsp; &nbsp; }&nbsp; &nbsp; private void updateDialogTitle(TimePicker timePicker, int hourOfDay, int minute) {&nbsp; &nbsp; &nbsp; &nbsp; calendar.set(Calendar.HOUR_OF_DAY, hourOfDay);&nbsp; &nbsp; &nbsp; &nbsp; calendar.set(Calendar.MINUTE, minute);&nbsp; &nbsp; &nbsp; &nbsp; String title = dateFormat.format(calendar.getTime());&nbsp; &nbsp; &nbsp; &nbsp; setTitle(title);&nbsp; &nbsp; }&nbsp; &nbsp; private void fixSpinner(Context context, int hourOfDay, int minute, boolean is24HourView) {&nbsp; &nbsp; &nbsp; &nbsp; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { // android:timePickerMode spinner and clock began in Lollipop&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Get the theme's android:timePickerMode&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //two modes are available clock mode and spinner mode ... selecting spinner mode for latest versions&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int MODE_SPINNER = 2;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<?> styleableClass = Class.forName("com.android.internal.R$styleable");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field timePickerStyleableField = styleableClass.getField("TimePicker");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int[] timePickerStyleable = (int[]) timePickerStyleableField.get(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final TypedArray a = context.obtainStyledAttributes(null, timePickerStyleable, android.R.attr.timePickerStyle, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field timePickerModeStyleableField = styleableClass.getField("TimePicker_timePickerMode");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int timePickerModeStyleable = timePickerModeStyleableField.getInt(null);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; final int mode = a.getInt(timePickerModeStyleable, MODE_SPINNER);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; a.recycle();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (mode == MODE_SPINNER) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; TimePicker timePicker = (TimePicker) findField(TimePickerDialog.class, TimePicker.class, "mTimePicker").get(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<?> delegateClass = Class.forName("android.widget.TimePicker$TimePickerDelegate");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field delegateField = findField(TimePicker.class, delegateClass, "mDelegate");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Object delegate = delegateField.get(timePicker);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Class<?> spinnerDelegateClass;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (Build.VERSION.SDK_INT != Build.VERSION_CODES.LOLLIPOP) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerDelegateClass = Class.forName("android.widget.TimePickerSpinnerDelegate");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerDelegateClass = Class.forName("android.widget.TimePickerClockDelegate");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (delegate.getClass() != spinnerDelegateClass) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delegateField.set(timePicker, null); // throw out the TimePickerClockDelegate!&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timePicker.removeAllViews(); // remove the TimePickerClockDelegate views&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Constructor spinnerDelegateConstructor = spinnerDelegateClass.getConstructor(TimePicker.class, Context.class, AttributeSet.class, int.class, int.class);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; spinnerDelegateConstructor.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Instantiate a TimePickerSpinnerDelegate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delegate = spinnerDelegateConstructor.newInstance(timePicker, context, null, android.R.attr.timePickerStyle, 0);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; delegateField.set(timePicker, delegate); // set the TimePicker.mDelegate to the spinner delegate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; // Set up the TimePicker again, with the TimePickerSpinnerDelegate&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timePicker.setIs24HourView(is24HourView);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timePicker.setCurrentHour(hourOfDay);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timePicker.setCurrentMinute(minute);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; timePicker.setOnTimeChangedListener(this);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; throw new RuntimeException(e);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; private static Field findField(Class objectClass, Class fieldClass, String expectedName) {&nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Field field = objectClass.getDeclaredField(expectedName);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; field.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return field;&nbsp; &nbsp; &nbsp; &nbsp; } catch (NoSuchFieldException e) {} // ignore&nbsp; &nbsp; &nbsp; &nbsp; // search for it if it wasn't found under the expected ivar name&nbsp; &nbsp; &nbsp; &nbsp; for (Field searchField : objectClass.getDeclaredFields()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (searchField.getType() == fieldClass) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; searchField.setAccessible(true);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return searchField;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return null;&nbsp; &nbsp; }}

米脂

您可以以此为起点。我扩展了TimePickerDialog并添加了2个方法setMin和setMax。在onTimeChanged方法中,我检查新时间相对于最小/最大时间是否有效。它仍然需要抛光...public class BoundTimePickerDialog extends TimePickerDialog {&nbsp; &nbsp; private int minHour = -1, minMinute = -1, maxHour = 100, maxMinute = 100;&nbsp; &nbsp; private int currentHour, currentMinute;&nbsp; &nbsp; public BoundTimePickerDialog(Context context, OnTimeSetListener callBack, int hourOfDay, int minute, boolean is24HourView) {&nbsp; &nbsp; &nbsp; &nbsp; super(context, callBack, hourOfDay, minute, is24HourView);&nbsp; &nbsp; }&nbsp; &nbsp; public void setMin(int hour, int minute) {&nbsp; &nbsp; &nbsp; &nbsp; minHour = hour;&nbsp; &nbsp; &nbsp; &nbsp; minMinute = minute;&nbsp; &nbsp; }&nbsp; &nbsp; public void setMax(int hour, int minute) {&nbsp; &nbsp; &nbsp; &nbsp; maxHour = hour;&nbsp; &nbsp; &nbsp; &nbsp; maxMinute = minute;&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {&nbsp; &nbsp; &nbsp; &nbsp; super.onTimeChanged(view, hourOfDay, minute);&nbsp; &nbsp; &nbsp; &nbsp; boolean validTime;&nbsp; &nbsp; &nbsp; &nbsp; if(hourOfDay < minHour) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validTime = false;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(hourOfDay == minHour) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validTime = minute >= minMinute;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else if(hourOfDay == maxHour) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validTime = minute <= maxMinute;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; validTime = true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; if(validTime) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentHour = hourOfDay;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; currentMinute = minute;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateTime(currentHour, currentMinute);&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Android