在Android 裡挑選時間和日期,官方有範例:
https://developer.android.com/guide/topics/ui/controls/pickers.html
非官方範例:
- https://www.tutorialspoint.com/android/android_datepicker_control.htm
- https://stackoverflow.com/questions/3299392/date-picker-in-android
設定 datepickerdialog max/min date:
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 :