在Android中设置DatePickerDialog的限制?

Dev*_*per 40 android android-datepicker datepickerdialog

如何限制DatePickerDialog选择过去2 - 12年或当前日期0 - 2年的日期?

我在活动中有DatePickerDialog,用于获取儿童和婴儿的出生日期.儿童年龄为(2-12岁),婴儿年龄为(0-2岁).如果我通过1,那么年龄限制应该是(2-12岁),如果我通过2岁,年龄限制应该是(0-2岁).

我该怎么做才能在我的日历中获得此功能?

我的日历代码是:

date = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                myCalendar.set(Calendar.YEAR, year);
                myCalendar.set(Calendar.MONTH, monthOfYear);
                myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
                String myFormat = "dd/MM/yy"; //In which you need put here
                SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
                dateofBirth.setText(sdf.format(myCalendar.getTime()));
            }
        };

dateofBirth.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                showDialog(DATE_DIALOG_ID);

            }
        });

public Dialog onCreateDialog(int id) {
    final Calendar now = Calendar.getInstance();

    switch (id) {
    case DATE_DIALOG_ID:
        // set date picker as current date
        DatePickerDialog _date =   new DatePickerDialog(this, date,myCalendar
                .get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
                myCalendar.get(Calendar.DAY_OF_MONTH)){
            @Override

            public void onDateChanged(DatePicker view, int year, int monthOfYear, int dayOfMonth){   

                if (year > now.get(Calendar.YEAR))

                    view.updateDate(myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                if (monthOfYear > now.get(Calendar.MONTH) && year == now.get(Calendar.YEAR))
                    view.updateDate(myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));

                if (dayOfMonth > now.get(Calendar.DAY_OF_MONTH) && year == now.get(Calendar.YEAR) && 
                        monthOfYear == now.get(Calendar.MONTH))
                    view.updateDate(myCalendar
                            .get(Calendar.YEAR), myCalendar
                            .get(Calendar.MONTH), myCalendar.get(Calendar.DAY_OF_MONTH));
            }

        };

        return _date;
    }
    return null;
}
Run Code Online (Sandbox Code Playgroud)

MH.*_*MH. 170

所有其他答案看起来相当复杂,所以我只是说明一点:你可以DatePickerDatePickerDialog(通过简单调用getDatePicker())得到底层并使用以下方法设置其边界:

其中参数是自默认时区1970年1月1日00:00:00以来的通常毫秒数.当然,您仍然需要计算这些值,但这对于类来说应该是微不足道的Calendar:只需要使用当前日期并添加或减去x年份.

更细心的读者会注意到,前面提到的setter在API级别11之前是不可用的.如果你只针对那个(或更新的)平台,那么你很高兴.如果您还想支持即姜饼设备(Android 2.3/API等级9+),您可以使用反向移植版本DatePicker.

  • 这就是这样做的方式:http://stackoverflow.com/questions/13661788/how-to-set-minimum-datepicker-date-to-current-date (4认同)