2013-01-16 80 views
0

在我的應用程序中我有editText,點擊它調用openDatePicker()函數。android設置日期的最大值<API級別11

protected void openDatePicker() { 
    DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() { 
     public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) { 
      GregorianCalendar calendar = new GregorianCalendar(); 
      calendar.set(Calendar.YEAR, year); 
      calendar.set(Calendar.MONTH, monthOfYear); 
      calendar.set(Calendar.DATE, dayOfMonth); 
      Date birthDate = calendar.getTime(); 
      setDate(birthDate); 
     } 

    }; 
    DatePickerDialog dialog = prepareDatePickerDialg(mDateSetListener); 
    dialog.show(); 
} 

當用戶選擇日期時,我必須設置最大值。那麼有人可以給我一些建議嗎?感謝

+0

有一種方法,但它不是很漂亮,你必須擴展DatePickerDialog ... – dmon

+0

如果你的目標是API級別11以上,你可以從對話框中獲得DatePicker實例並調用setMaxDate(long) '在上面。對於較老的設備,另一種方法是使用backported版本,[像這樣](https://github.com/SimonVT/android-datepicker)(儘管它也有一些缺點)。 –

+0

非常感謝,並請回答您的意見:) –

回答

1

按照先前的評論:

如果你的目標API級別11,你可以從對話框獲得DatePicker實例,並在其上調用setMaxDate(long)。對於較舊的設備,另一種方法是使用backported版本,like this one(儘管它也有一些缺點)。

+0

謝謝,但我試過了,它不能很好地工作:/ –

+0

而且*「工作不好」*您的意思是...? –

+0

我的意思是我將它作爲單個應用程序來運行,並且當我按下名爲「暗黑主題」和「光主題」的按鈕時,它會崩潰, –

相關問題