[Android] 日期挑選

Posted in :

在Android 裡挑選時間和日期,官方有範例:

https://developer.android.com/guide/topics/ui/controls/pickers.html

 

非官方範例:

 

設定 datepickerdialog max/min date:

https://stackoverflow.com/questions/33051236/android-datepickerdialog-set-min-and-max-date-for-selection

Use setMaxDate().

For example, replace return new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay) statement with something like this:

    DatePickerDialog dialog = new DatePickerDialog(this, pDateSetListener, pYear, pMonth, pDay);
    dialog.getDatePicker().setMaxDate(new Date().getTime());
    return dialog;
Calendar c = Calendar.getInstance();
c.set(2017, 0, 1);//Year,Mounth -1,Day
your_date_picker.setMaxDate(c.getTimeInMillis());

getTimeInMillis 需要 API 24, 所以需要改用 java.util.Calendar

Instead of importing android.icu.util.calendar, try importing java.util.Calendar.

The android.icu.util.calendar is the ICU replacement for java.util.Calendar but it’s only available starting from API 24. So, it will only work on the devices which is API 24 and above.

Ref :

 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *