DatePicker的Android日历视图

时间:2014-04-08 06:16:40

标签: android android-calendar android-datepicker

我正在使用日期选择器来设置当前日期。当在nexus 7中打开视图时,与日期微调器一起我们可以看到日历。日期微调器正确设置为今天,因为日历视图显示2100年。

DatePickerDialog tempDialog = new DatePickerDialog(getActivity(), this, cur_year, cur_month, cur_day);
tempDialog.getDatePicker().setMinDate(today.getTimeInMillis());

as you see date spinner is proper, but calendar view is on year 2100 november

1 个答案:

答案 0 :(得分:0)

为日历dailog启动使用init。

        final Calendar calendarRange = Calendar.getInstance();
        final int maxYear = calendarRange.get(Calendar.YEAR);
        final int maxMonth = calendarRange.get(Calendar.MONTH);
        final int maxDay = calendarRange.get(Calendar.DAY_OF_MONTH);

        tempDialog.init(maxYear, maxMonth, maxDay,
                new OnDateChangedListener() {

                    public void onDateChanged(DatePicker view, int year,
                            int month, int day) {
                        Calendar newDate = Calendar.getInstance();
                        newDate.set(year, month, day);

                        if (calendarRange.before(newDate)) {
                            view.init(maxYear, maxMonth, maxDay, this);
                        }

                    }
                });

然后使用此代码:

Calendar cal = Calendar.getInstance();
int maxYear = cal.get(Calendar.YEAR);
int maxMonth = cal.get(Calendar.MONTH);
int maxDay = cal.get(Calendar.DAY_OF_MONTH);

DatePickerDialog dialog =
    new DatePickerDialog(this, mDateSetListener, maxYear , maxMonth , maxDay);
dialog.show();