setMinDate(...)的调用第二次时的DatePicker不工作 [英] setMinDate(...) for DatePicker doesn't work when invoked a second time

查看:637
本文介绍了setMinDate(...)的调用第二次时的DatePicker不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在我需要改变的DatePicker 根据一个选定的元素微调的最小和最大日期的具体情况。下面是我使用切换的最小和最大日期code的份额。

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 the chunk of code I'm using to switch min and max date.

private void switchCalculationMethod(int method) {
    calculationMethod = method;
    switch (method) {
        case METHOD_1:
            datePicker.setMinDate(new LocalDate().minusWeeks(42).getMillis());
            datePicker.setMaxDate(new LocalDate().plusDays(1).getMillis() - 1);
            break;
        case METHOD_2:
            datePicker.setMinDate(new LocalDate().minusWeeks(2).getMillis()); // This don't work!!
            datePicker.setMaxDate(new LocalDate().plusWeeks(40).getMillis()); // This works!!!
            break;
    }
    datePicker.init(today.getYear(), today.getMonthOfYear() - 1,
            today.getDayOfMonth(), this);
}

因此​​,的DatePicker 将得到正确设置的第一次,当我尝试再次更改日期最小(最大改变日期作品)出现问题。它会留在我第一次设置的值。我想这是一个错误。难道我做错了什么吗?是否有解决方法吗?

So, the DatePicker would get set up correctly the first time, problem occurs when I attempt to change the min date again (changing max date works). It would remain at the value I had set first. I'm thinking this is a bug. Am I doing something wrong here? Is there a workaround for this?.

PS:我使用的乔达时间API

PS : I'm using Joda time api.

推荐答案

这是因为方法setMinDate()有检查

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());

这为我工作。

这篇关于setMinDate(...)的调用第二次时的DatePicker不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆