禁用Android TimePicker的自动更改时间

时间:2019-03-30 13:40:34

标签: java android timepicker

如果将TimePicker的小时设置为7:59,然后将分钟向下滚动到00,则小时会自动更改为8,现在是8:00。有没有办法禁用该行为?我希望在滚动分钟以传递00和59值时,小时数不会改变。

我正在使用TimePicker作为视图

<TimePicker
        android:id="@+id/timePicker_ID"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:timePickerMode="spinner"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

编辑:

这有点奏效。不会将其发布为答案,因为我不确定它是否在每种情况下都可以正常工作。

private int oldHour = -1;
private int oldMinute = -1;

...

timePicker.setOnTimeChangedListener(new TimePicker.OnTimeChangedListener() {
            @Override
            public void onTimeChanged(TimePicker timePicker, int hour, int minute) {

                if(oldMinute != -1 && oldHour != -1){
                    if((oldMinute == 59 && minute == 00) ||(oldMinute == 00 && minute == 59)) {
                        hour = oldHour;
                        // Endless loop possibility below
                        if (Build.VERSION.SDK_INT <= 23) {
                            timePicker.setCurrentHour(hour);
                        } else {
                            timePicker.setHour(hour);
                        }
                    }
                }

                oldMinute = minute;
                oldHour = hour;
                ...

0 个答案:

没有答案