setMinDate() for DatePicker doesn't work

◇◆丶佛笑我妖孽 提交于 2020-01-03 17:08:46

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!