滚动分钟

时间:2018-02-15 06:48:51

标签: java android time android-timepicker timepickerdialog

当我滚动分钟时,Android Timepickerdialog正在改变不合时宜的时间。我在timepickerdialog中将分钟间隔设置为30分钟,因此分钟的值为00和30.例如,当我将小时数设置为00并将分钟数从00更改为30时,小时数减少一小时所以时间现在是23:30我想要的是00:30。我意识到这不会发生在三星设备上,而是发生在其他所有制造商的设备上。滚动分钟时有没有办法禁用小时数变化?

这是我的代码

public class CustomTimePickerDialog extends TimePickerDialog {

    private int TIME_PICKER_INTERVAL = 30;
    private TimePicker mTimePicker;
    private final OnTimeSetListener mTimeSetListener;
    public int interval1 = 1;

    public CustomTimePickerDialog(Context context, OnTimeSetListener listener,
                                  int hourOfDay, int minute, boolean is24HourView, int interval) {

        super(context, TimePickerDialog.THEME_HOLO_LIGHT, null, hourOfDay,
                minute / interval, is24HourView);

        TIME_PICKER_INTERVAL = interval;

        mTimeSetListener = listener;

    }

    @Override
    public void updateTime(int hourOfDay, int minuteOfHour) {
        mTimePicker.setCurrentHour(hourOfDay);
        mTimePicker.setCurrentMinute(minuteOfHour / TIME_PICKER_INTERVAL);

    }

    @Override
    public void onClick(DialogInterface dialog, int which) {
        switch (which) {
            case BUTTON_POSITIVE:
                if (mTimeSetListener != null) {
                    mTimeSetListener.onTimeSet(mTimePicker, mTimePicker.getCurrentHour(),
                            mTimePicker.getCurrentMinute() * TIME_PICKER_INTERVAL);
                }
                break;
            case BUTTON_NEGATIVE:
                cancel();
                break;
        }
    }

    @Override
    public void onAttachedToWindow() {
        super.onAttachedToWindow();
        try {
            Class<?> classForid = Class.forName("com.android.internal.R$id");
            Field timePickerField = classForid.getField("timePicker");
            mTimePicker = (TimePicker) findViewById(timePickerField.getInt(null));
            Field field = classForid.getField("minute");

            NumberPicker minuteSpinner = (NumberPicker) mTimePicker
                    .findViewById(field.getInt(null));
            minuteSpinner.setMinValue(0);
            minuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
            List<String> displayedValues = new ArrayList<String>();
            for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
                displayedValues.add(String.format("%02d", i));
            }
            minuteSpinner.setDisplayedValues(displayedValues
                    .toArray(new String[displayedValues.size()]));
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

我知道至少有一个类似的问题,但它没有解决问题的答案。 Android time picker auto change hours when minutes get scrolled

编辑:我也注意到,当我将分钟间隔改为15时,它的工作效果很好。例如,当我将分钟数从45增加到00时,小时数增加1。同样的事情,当我将分钟从00减少到45时,小时数减少一个。当分钟间隔为6时也是如此。所以问题是只有30分钟的时间间隔,我必须在我的软件中有30分钟的间隔。

0 个答案:

没有答案