问题描述
我是想设定小时和分钟的 TimePicker
,我发现,它似乎有在 NumberPicker 的值环绕。
I was trying to set the hour and minute in the TimePicker
and I found that it seems that there is a minimum number of items required in the NumberPicker
for the values to wrap around.
如何设置的项目数比,如果我没有那么多值默认较小(只拿到了4:00,15,30和45)?
How do i set the number of items to lesser than the default if i don't have so many values (got only 4: "00", "15", "30" and "45")?
下面的图像显示小时,正确的,但纪要错误的。我需要的分钟数(00之前,即45),以环绕像小时。
The image below shows the hours as correct but the minutes as wrong. I need the minutes to wrap around (i.e. "45" before "00") like the hours.
下面是我的code:
@SuppressLint("NewApi")
private void setTimePickerInterval(TimePicker timePicker) {
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field fieldHr = classForid.getField("hour");
mHourPicker = (NumberPicker) timePicker.findViewById(fieldHr.getInt(null));
//set hours from 9am to 7pm (opening hours)
mHourPicker.setMinValue(0);
mHourPicker.setMaxValue(10);
mDisplayedValuesHr = new ArrayList<String>();
for (int i = 9; i < 20; i++) {
mDisplayedValuesHr.add(String.format("%02d", i));
}
mHourPicker.setDisplayedValues(mDisplayedValuesHr.toArray(new String[0]));
Field fieldMin = classForid.getField("minute");
mMinutePicker = (NumberPicker) timePicker.findViewById(fieldMin.getInt(null));
//set minutes in 15 mins interval
mMinutePicker.setMinValue(0);
mMinutePicker.setMaxValue(3);
mDisplayedValuesMin = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
mDisplayedValuesMin.add(String.format("%02d", i));
}
mMinutePicker.setDisplayedValues(mDisplayedValuesMin.toArray(new String[0]));
} catch (Exception e) {
e.printStackTrace();
}
}
感谢
推荐答案
您可以显式调用 setWrapSelectorWheel(布尔)触发 updateWrapSelectorWheel
NumberPicker
:
You can explicitly call setWrapSelectorWheel (boolean) to trigger the private method updateWrapSelectorWheel
of NumberPicker
:
@SuppressLint("NewApi")
private void setTimePickerInterval(TimePicker timePicker) {
try {
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field fieldHr = classForid.getField("hour");
mHourPicker = (NumberPicker) timePicker.findViewById(fieldHr.getInt(null));
//set hours from 9am to 7pm (opening hours)
mHourPicker.setMinValue(0);
mHourPicker.setMaxValue(10);
mDisplayedValuesHr = new ArrayList<String>();
for (int i = 9; i < 20; i++) {
mDisplayedValuesHr.add(String.format("%02d", i));
}
mHourPicker.setDisplayedValues(mDisplayedValuesHr.toArray(new String[0]));
Field fieldMin = classForid.getField("minute");
mMinutePicker = (NumberPicker) timePicker.findViewById(fieldMin.getInt(null));
//set minutes in 15 mins interval
mMinutePicker.setMinValue(0);
mMinutePicker.setMaxValue(3);
mDisplayedValuesMin = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
mDisplayedValuesMin.add(String.format("%02d", i));
}
mMinutePicker.setDisplayedValues(mDisplayedValuesMin.toArray(new String[0]));
mMinutePicker.setWrapSelectorWheel(true);
} catch (Exception e) {
e.printStackTrace();
}
}
这篇关于项目的Android NumberPicker组数字环绕项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!