如何更改Android TimePicker分钟间隔?

时间:2013-04-24 08:40:47

标签: android timepicker minute

我正在编写一个应用程序,用户需要指定一个给定的时间点,但我似乎无法弄清楚如何设置用户可以选择的分钟值,只使用5的增量而不是增量为1。

简单地说,当用户滚动可用金额时,他/她必须只看到0,5,10,15等。

提前谢谢。

6 个答案:

答案 0 :(得分:4)

为确保您与API 21+兼容,请确保您的TimePicker具有以下属性:

android:timePickerMode="spinner"

然后是如何以编程方式设置间隔的方法。如果找不到分钟字段,此方法将回退到标准TimePicker:

private static final int INTERVAL = 5;
private static final DecimalFormat FORMATTER = new DecimalFormat("00");

private TimePicker picker; // set in onCreate
private NumberPicker minutePicker;

public void setMinutePicker() {
    int numValues = 60 / INTERVAL;
    String[] displayedValues = new String[numValues];
    for (int i = 0; i < numValues; i++) {
        displayedValues[i] = FORMATTER.format(i * INTERVAL);
    }

    View minute = picker.findViewById(Resources.getSystem().getIdentifier("minute", "id", "android"));
    if ((minute != null) && (minute instanceof NumberPicker)) {
        minutePicker = (NumberPicker) minute;
        minutePicker.setMinValue(0);
        minutePicker.setMaxValue(numValues - 1);
        minutePicker.setDisplayedValues(displayedValues);
    }
}

public int getMinute() {
    if (minutePicker != null) {
        return (minutePicker.getValue() * INTERVAL);
    } else {
        return picker.getCurrentMinute();
    }
}

答案 1 :(得分:3)

所有相关答案都需要您设置OnTimeChangedListener。 我的解决方案是你扩展android TimePicker,并修改它的构造函数:

    // minute
    mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
    mMinuteSpinner.setMinValue(0);
    mMinuteSpinner.setMaxValue(3);
    mMinuteSpinner.setDisplayedValues(new String[]{"0", "15", "30", "45"});
    mMinuteSpinner.setOnLongPressUpdateInterval(100);
    mMinuteSpinner.setFormatter(NumberPicker.getTwoDigitFormatter());

所以你可以得到你想要的间隔。

答案 2 :(得分:1)

感谢@Quadddd,这是Kotlin中的代码

private val INTERVAL = 5


private val FORMATTER = DecimalFormat("00")

  private var picker: TimePicker? = null // set in onCreate
  private var minutePicker: NumberPicker? = null

  fun setMinutePicker() {
    val numValues = 60 / INTERVAL
    val displayedValues = arrayOfNulls<String>(numValues)
    for (i in 0 until numValues) {
      displayedValues[i] = FORMATTER.format(i * INTERVAL)
    }

    val minute = picker?.findViewById<NumberPicker>(Resources.getSystem().getIdentifier("minute", "id", "android"))
    if (minute != null) {
      minutePicker = minute
      minutePicker!!.minValue = 0
      minutePicker!!.maxValue = numValues - 1
      minutePicker!!.displayedValues = displayedValues
    }
  }

  fun getMinute(): Int {
    return if (minutePicker != null) {
      minutePicker!!.getValue() * INTERVAL
    } else {
      picker!!.currentMinute
    }
  }

答案 3 :(得分:0)

您可以通过编程方式执行此操作。

pickStartTime = (TimePicker)findViewById(R.id.StartTime);
pickStartTime.setOnTimeChangedListener(mStartTimeChangedListener);
int nextMinute = 0;

接下来,设置OnTimeChangedListener,如下所示

private TimePicker.OnTimeChangedListener mStartTimeChangedListener =
    new TimePicker.OnTimeChangedListener() {
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
            updateDisplay(view, startDate, hourOfDay, minute);          
        }
    };

private TimePicker.OnTimeChangedListener mNullTimeChangedListener =
    new TimePicker.OnTimeChangedListener() {
        public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {}
    };

private void updateDisplay(TimePicker timePicker, Date date, int hourOfDay, int minute) 
{ 
    nextMinute = nextMinute+5;

    // remove ontimechangedlistener to prevent stackoverflow/infinite loop
    timePicker.setOnTimeChangedListener(mNullTimeChangedListener);

    // set minute
    timePicker.setCurrentMinute(nextMinute);

    // look up ontimechangedlistener again
    timePicker.setOnTimeChangedListener(mStartTimeChangedListener);

    // update the date variable for use elsewhere in code
    date.setMinutes(nextMinute);  
}

答案 4 :(得分:0)

搜索网后,我找不到最简单的解决方案。
所以我决定分享:
你可以使用反射!
以下代码使用10分钟的间隔,但您可以在DISPLAYED_MINS数组中选择要使用的分钟。

private final static String[] DISPLAYED_MINS = { "0", "10", "20", "30", "40", "50" };

private NumberPicker getMinuteSpinner(TimePicker t)
{
    try
    {
        Field f = t.getClass().getDeclaredField("mMinuteSpinner"); // NoSuchFieldException
        f.setAccessible(true);
        return (NumberPicker) f.get(t); // IllegalAccessException
    }
    catch (Exception e)
    {
        Log.e("timepicker","field name has been changed, check grepcode");
        return null;
    }
}

创建时间戳时:

final TimePicker dpStartDate = (TimePicker) view.findViewById(R.id.dpStartDate);
NumberPicker startMinSpiner = getMinuteSpinner(dpStartDate);
if (null != startMinSpiner)
{
    startMinSpiner.setMinValue(0);
    startMinSpiner.setMaxValue(DISPLAYED_MINS.length - 1);
    startMinSpiner.setDisplayedValues(DISPLAYED_MINS);
}

请不要忘记getCurrentMinute()返回DISPLAY_MINS数组中的选定分钟。 为了得到分钟:

int theTime = 0;
for (String number: DISPLAYED_MINS) {
    int theNumber = Integer.parseInt(number);
    if (theNumber % 5 != 0) {
        theNumber = ((int)(theNumber / 5)) * 5;
    }
    if (theNumber == dpStartDate.getCurrentMinute()) {
        theTime = theNumber;
        break;
    }
}

答案 5 :(得分:0)

作为@Andrey Kotlin解决方案的后续操作,您可以扩展它以指定任何给定的分钟间隔。另外,使用kotlin范围和 step 关键字,您可以得到一些更高级的代码:

private fun setupMinutesPicker(minVal: Int, maxVal: Int) {
    minsNumberPicker.displayedValues = null

    minsNumberPicker.minValue = minVal / MINUTE_INTERVAL_STEP
    minsNumberPicker.maxValue = maxVal / MINUTE_INTERVAL_STEP

    minsNumberPicker.displayedValues = (minVal..maxVal step MINUTE_INTERVAL_STEP)
        .toList()
        .map { mins -> FORMATTER.format(mins) }
        .toTypedArray()
}