我写在用户需要指定在给定时间点的应用程序,但我似乎无法弄清楚如何设置分钟值,用户可以从中选择,只用了5增量,而不是增量为1 。
简单地说,当通过可用量的用户滚动,他/她必须只看到0,5,10,15等。
先感谢您。
我写在用户需要指定在给定时间点的应用程序,但我似乎无法弄清楚如何设置分钟值,用户可以从中选择,只用了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();
}
}
所有相关答案需要你设置一个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());
所以你可以有你想要的时间间隔。
由于@Quadddd ,这是科特林代码
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
}
}
搜索网后,我没有找到简单的解决方案。
所以我决定分享到:
您可以使用反射!
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;
}
}
当您创建timepicker:
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;
}
}
您可以以编程方式做到这一点。
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);
}