如何更改Android TimePicker分钟间隔?
作者:互联网
我正在编写一个应用程序,用户需要指定一个给定的时间点,但我似乎无法弄清楚如何设置用户可以选择的分钟值,只使用5的增量而不是1的增量.
简单地说,当用户滚动可用金额时,他/她必须只看到0,5,10,15等.
先感谢您.
解决方法:
为确保您与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();
}
}
标签:timepicker,android,minute 来源: https://codeday.me/bug/20191005/1854985.html