问题
DatePicker datepicker = (DatePicker) view.findViewById(R.id.ad_date_picker);
datepicker.setMinDate(Calendar.getInstance().getTimeInMillis());
It just doesn't work. It works only in the xml (android:minDate="mm/dd/yyyy"), but I need to set the minimum date dynamically to the current day.
My minimum API is 11.
回答1:
P.S: Solution used to work before but might be outdated now.
Try something like this. If I understand correctly, you wish to set the minimum date of spinner to today's date. It works for me without giving any error:
Calendar cal = Calendar.getInstance();
cal.set(cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH), 0, 0, 0);
long time = cal.getTimeInMillis();
DatePicker datepicker = (DatePicker) findViewById(R.id.datePicker);
datepicker.setMinDate(time);
In your xml I am supposing that you have something like this:
<DatePicker
android:id="@+id/datePicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
来源:https://stackoverflow.com/questions/15465330/setmindate-for-datepicker-doesnt-work