setMinDate(…) for DatePicker doesn't work when invoked a second time

后端 未结 4 1107
终归单人心
终归单人心 2021-01-07 23:41

I\'m in a particular situation in which I have to alter the min and max date of DatePicker according to the selected element of a Spinner. Here\'s

4条回答
  •  别那么骄傲
    2021-01-07 23:43

    This happens because method setMinDate() has check

     if (mTempDate.get(Calendar.YEAR) == mMinDate.get(Calendar.YEAR)
                    && mTempDate.get(Calendar.DAY_OF_YEAR) != mMinDate.get(Calendar.DAY_OF_YEAR){
                return;
     }
    

    Simple workaround is to set min date with different year at first, for example

    mPicker.setMinDate(0);
    
    mPicker.setMinDate(new LocalDate().minusWeeks(2)
                                    .toDateTimeAtStartOfDay().getMillis());
    

    It works for me.

提交回复
热议问题