轮日期选择器中的一天更改有问题

时间:2014-03-25 09:05:39

标签: android datepicker

在我的车轮日期选择器中,一个问题出现在28,29,30或31个月。现在,没有看到这个问题。要检查错误, (1)请在2014年3月31日或2014年1月30日更改模拟器的日期......就像这样。 (2)运行代码,你会看到二月和几个月出现问题。Here is the source code

这是我的代码......

Calendar updateDays(WheelView year, WheelView month, WheelView day) {

        Calendar calendar = Calendar.getInstance();

        if(Number == 0){
            calendar.set(Calendar.YEAR,1900+year.getCurrentItem());
        }else{
            calendar.set(Calendar.YEAR,+curYear+year.getCurrentItem());
        }

        calendar.set(Calendar.MONTH, month.getCurrentItem());

        int maxDays = calendar.getActualMaximum(Calendar.DAY_OF_MONTH);

        //day.setViewAdapter(new DateNumericAdapter(Mcontex, 1, maxDays, calendar.get(Calendar.DAY_OF_MONTH) - 1));
        day.setViewAdapter(new NumericWheelAdapter(Mcontex, 1, maxDays, "%01d Day"));

        int curDay = Math.min(maxDays, day.getCurrentItem() + 1);

        day.setCurrentItem(curDay - 1, true);
        calendar.set(Calendar.DAY_OF_MONTH, curDay);

        Log.i("curDay",curDay+"");
        Log.i("maxDays",maxDays+"");
        Log.i("Calendar.DayOFMonth",Calendar.DAY_OF_MONTH+"");

        return calendar;

}

1 个答案:

答案 0 :(得分:0)

我刚解决了这个问题......如果有人得到同样的错误,这可能是帮助...:)

    Calendar updateDays(WheelView year, WheelView month, WheelView day) {

    Calendar calendar = Calendar.getInstance();
    int maxDays = 0 ;

    if(Number == 0){
        calendar.set(Calendar.YEAR,1900+year.getCurrentItem());
    }else{
        calendar.set(Calendar.YEAR,+curYear+year.getCurrentItem());
    }

    calendar.set(Calendar.MONTH, month.getCurrentItem());
    int YearChanging= 1900+ year.getCurrentItem();

    switch(month.getCurrentItem()){
    case 3:
    case 5:
    case 8:
    case 10: maxDays= 30; break;

    case 0:
    case 2:
    case 4:
    case 6:
    case 7:
    case 9:
    case 11: maxDays=31; break;

    case 1:
        if ((YearChanging % 400 == 0) || ((YearChanging % 4 == 0) && (YearChanging % 100 != 0))) {
            maxDays =29 ;
        } else {
            maxDays = 28 ;
        }
        break;
    }

    day.setViewAdapter(new NumericWheelAdapter(Mcontex, 1, maxDays, "%01d Day"));

    int curDay = Math.min(maxDays, day.getCurrentItem() + 1);

    day.setCurrentItem(curDay - 1, true);
    calendar.set(Calendar.DAY_OF_MONTH, curDay);

    return calendar;

}