這就是我的時間選擇器現在的樣子。 我希望它只顯示分鐘和分鐘只能達到最大值30. 它工作正常,但是當你用鍵盤輸入時間時,它將時間設置爲0,但我希望它達到最大值。 所以如果用戶輸入50,它應該去30,我該怎麼做? 以及如何隱藏timepicker的小時,因爲這裏不需要它。如果用戶使用鍵盤輸入時間,TimePicker僅顯示分鐘數並且設置最大分鐘數
protected void onCreate(Bundle savedInstanceState) {
System.out.println("Powernap");
super.onCreate(savedInstanceState);
setContentView(R.layout.powernap);
TimePicker pn = (TimePicker) findViewById(R.id.tp_powernap);
pn.setOnTimeChangedListener(StartTimeChangedListener);
pn.setIs24HourView(true);
}
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) {
// do calculation of next time
int nextMinute = 0;
int nextHour = 0;
if (minute > 30){
System.out.println("Minute+30");
nextMinute = 30;
// set minute
timePicker.setCurrentMinute(nextMinute);
}
else {
System.out.println("minute 0");
nextMinute = 0;
}
if (hourOfDay != 0){
System.out.println("hour>0");
nextHour = 24;
}
// remove ontimechangedlistener to prevent stackoverflow/infinite loop
timePicker.setOnTimeChangedListener(NullTimeChangedListener);
// set minute
timePicker.setCurrentMinute(timePicker.getCurrentMinute()+nextMinute);
//set hour
timePicker.setCurrentHour(nextHour);
// hook up ontimechangedlistener again
timePicker.setOnTimeChangedListener(StartTimeChangedListener);
}
只隱藏小時,或者也只顯示分鐘,30一件事我想要做的就是與按鈕只顯示5,10,15,20,25和30分鐘上下。我只找到一個解決方案,用up或down來完成,而另一個則不行。 – kirbby 2011-01-31 10:28:29