Android DatePicker在日历上方显示错误的年份

时间:2013-04-11 15:45:52

标签: android

我正在尝试创建日期选择器片段,并且遇到了日历上方的年份未正确更新的问题。

我遇到的第一个问题是,如果我使用上/下年份按钮,片段的标题会更改为“设置日期”,而日历上方的年份不会更新(日期网格会更新)。

第二个问题是当我打开日期以及未来的日期或过去日期中显示的日期和日历上方的日期是非常错误的。

未来的断点发生在2037/4/11和2038/4/11之间。虽然我没有追捕到确切的一天,但我怀疑潜伏在后台的32位unix时间是以某种方式参与的。

过去但是在1902年4月11日到1901年4月11日之间发生了中断;它与32位时间戳的开头不对齐,但确实对应于进入2038时看到的无效日期。

这种情况发生在我用于测试的4.1平板电脑以及4.1和4.2 AVD中。屏幕截图是从平板电脑AVD裁剪的。

此时我想知道我做错了什么,或者在操作系统本身发现了一个错误。

初始状态,今天载入: Initial state, loaded with today

按下+年按钮后,日历上方错误的标题年份未更新:
After pressing the + year button

加载2037日期,初始状态良好: Loaded with a 2037 date

加载2038日期,注意时间在标题和日历上方回到1902年: Loaded with a 2038 date

装载1902年的日期,没有错: Loaded with a 1902 date

加载了1901年的日期,在标题和日历上方注意时间扭曲到2037年: Loaded with a 1901 date

我的片段类:

public class DatePickerFragment extends DialogFragment {
    private static int _month = 0;
    private static int _day = 0;
    private static int _year = 0;
    private static int _buttonId = 0;

    public DatePickerFragment(){
    }

    public static final DatePickerFragment newInstance(int buttonId, int year, int month, int day) {
        DatePickerFragment f = new DatePickerFragment();
        _buttonId = buttonId;
        _year = year;
        _month = month;
        _day = day;
        return f;
    }

    @Override
    public Dialog onCreateDialog(Bundle savedInstanceState) {
        return new DatePickerDialog(getActivity(),  mDateSetListener, _year, _month, _day);
    }

    private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int year, int month, int day) {
                    //update the control whose press opened the DatePpickerFragment
            Button _button = (Button)getActivity().findViewById (_buttonId);
            if(_button != null) {
                _button.setText(month+1 + "/" + day + "/" + year);
            }
        }
    };
}

安装打开片段的按钮处理程序的代码:

    Button button = (Button) findViewById(buttonID);

    button.setOnClickListener( new View.OnClickListener() {
        public void onClick(View v) {

            Button button = (Button) v;
            DateFormat df = DateFormat.getDateInstance(DateFormat.SHORT);
            Calendar c = Calendar.getInstance();
            try {
                c.setTime(df.parse(button.getText().toString()));
            } catch (ParseException e) {
                // Do nothing. Calendar defaults to the current date. Use that instead.
            }

            int year = c.get(Calendar.YEAR);
            int month = c.get(Calendar.MONTH);
            int day = c.get(Calendar.DAY_OF_MONTH);

            final DatePickerFragment newFragment = DatePickerFragment.newInstance(button.getId(), year, month, day);
            newFragment.show(getFragmentManager(), "");
        }
    });
}

0 个答案:

没有答案