3
我正在使用DatePicker和TimePicker在android中。我從服務器獲取開始時間和時間。我可以使用this link如何在TimePicker Android中以30分鐘的間隔設置開始時間和結束時間?
以編程方式設置最大日期和最短日期我想在timepicker中設置最短時間(07:00)和最長時間(10:00)並保持30分鐘的時間間隔。我嘗試使用This Link
我所需要的是設置與timepicket分鐘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);
}