我的解决方法是继承android TimePicker 类
修改其构造函数
public TimePicker(Context context, AttributeSet attrs, int defStyle) {
// minute 的部分
mMinuteSpinner = (NumberPicker) findViewById(R.id.minute);
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue(3);
mMinuteSpinner.setDisplayedValues(new String[]{"0", "15", "30", "45"});
mMinuteSpinner.setOnLongPressUpdateInterval(100);
}
The following worked for me.
First, in onCreate:
pickStartTime = (TimePicker)findViewById(R.id.StartTime); pickStartTime.setOnTimeChangedListener(mStartTimeChangedListener);
Set the OnTimeChangedListener:
private TimePicker.OnTimeChangedListener mStartTimeChangedListener = new TimePicker.OnTimeChangedListener() { public void onTimeChanged(TimePicker view, int hourOfDay, int minute) { updateDisplay(view, startDate, hourOfDay, minute); } };
Null OnTimeChangedListener (explained in comments in updateDisplay below):
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) { // do calculation of next time int nextMinute = 0; if (minute >= 45 && minute <= 59) nextMinute = 45; else if(minute >= 30) nextMinute = 30; else if(minute >= 15) nextMinute = 15; else if(minute > 0) nextMinute = 0; else { nextMinute = 45; } // remove ontimechangedlistener to prevent stackoverflow/infinite loop timePicker.setOnTimeChangedListener(mNullTimeChangedListener); // set minute timePicker.setCurrentMinute(nextMinute); // hook up ontimechangedlistener again timePicker.setOnTimeChangedListener(mStartTimeChangedListener); // update the date variable for use elsewhere in code date.setMinutes(nextMinute); }