How do i change the Android TimePicker minute intervals?

后端 未结 6 1399
小鲜肉
小鲜肉 2020-12-15 14:36

I am writing an application where the user needs to specify a given point in time, but i can\'t seem to figure out how to set the minute values that the user can choose from

6条回答
  •  庸人自扰
    2020-12-15 14:58

    As a follow up on @Andrey Kotlin solution, you can make it extensible to specify any given minute interval. Also, using kotlin ranges and step keyword, you can have a bit fancier code:

    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()
    }
    

提交回复
热议问题