2011-12-24 95 views
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); 

} 

回答

0

僅供參考(這是一個老問題)。的間隔可以使用以下劈來實現:

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

開始 - 和endTime可以以類似的方式設置,但存在與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])); 
     } 
相關問題