我正在使用android中的DatePicker和TimePicker。我从服务器获得开始时间和时间。我可以使用this link
以编程方式设置最大日期和分钟日期我想在时间戳中设置最短时间(07:00)和最长时间(10:00)并保持30分钟的间隔。我尝试使用This Link
我需要的是在最小07:00和最长时间= 10:00和间隔时间设置时间表 07:00,07:30,08:00等。
这是相同的代码`
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);
pn.setCurrentHour(7);
pn.setCurrentMinute(0);
System.out.println("timepicker erstellt");
pn.setDescendantFocusability(TimePicker.FOCUS_BLOCK_DESCENDANTS);
System.out.println("Tastatur geblockt");
pn.setOnTimeChangedListener(StartTimeChangedListener);
System.out.println("starttimechangedlistener");
pn.setIs24HourView(false);
}
private TimePicker.OnTimeChangedListener StartTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
updateDisplay(view, hourOfDay, minute);
}
};
private TimePicker.OnTimeChangedListener NullTimeChangedListener =
new TimePicker.OnTimeChangedListener() {
public void onTimeChanged(TimePicker view, int hourOfDay, int minute) {
}
};
private void updateDisplay(TimePicker timePicker, int hourOfDay, int minute) {
int nextMinute = 0;
timePicker.setOnTimeChangedListener(NullTimeChangedListener);
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;
}
if (minute - nextMinute == 1) {
if (minute >= 45 && minute <= 59)
nextMinute = 00;
else if(minute >= 30)
nextMinute = 45;
else if(minute >= 15)
nextMinute = 30;
else if(minute > 0)
nextMinute = 15;
else {
nextMinute = 15;
}
}
timePicker.setCurrentMinute(nextMinute);
timePicker.setOnTimeChangedListener(StartTimeChangedListener);
}
答案 0 :(得分:0)
仅供参考(这是一个老问题)。可以使用以下hack来实现间隔:
Class<?> classForid = Class.forName("com.android.internal.R$id");
Field field = classForid.getField("minute");
NumberPicker mMinuteSpinner = (NumberPicker) timePicker
.findViewById(field.getInt(null));
mMinuteSpinner.setMinValue(0);
mMinuteSpinner.setMaxValue((60 / TIME_PICKER_INTERVAL) - 1);
List<String> displayedValues = new ArrayList<String>();
for (int i = 0; i < 60; i += TIME_PICKER_INTERVAL) {
displayedValues.add(String.format("%02d", i));
}
mMinuteSpinner.setDisplayedValues(displayedValues
.toArray(new String[0]));
另见here
可以以类似的方式设置开始和结束时间,但AM / PM标志存在问题。如果你不使用am / pm但是总是以24小时格式显示时间,你的问题就解决了; - )
if (startHour != -1 && endHour != -1) {
Field field = classForid.getField("hour");
NumberPicker hourSpinner = (NumberPicker) timePicker
.findViewById(field.getInt(null));
hourSpinner.setMinValue(startHour);
hourSpinner.setMaxValue(endHour);
List<String> displayedValues = new ArrayList<String>();
if (timePicker.is24HourView()) {
for (int i = startHour; i <= endHour; ++i) {
displayedValues.add(String.format("%02d", i));
}
} else {
for (int i = startHour; i <= 12; ++i) {
displayedValues.add(String.format("%d", i));
}
for (int i = 1; i <= endHour % 12; ++i) {
displayedValues.add(String.format("%d", i));
}
}
hourSpinner.setDisplayedValues(displayedValues
.toArray(new String[0]));
}