Android datepicker min max date before api level 11

前端 未结 7 533
生来不讨喜
生来不讨喜 2020-11-27 06:11

I am trying to set the min and max date of the date picker in Android to before API level 11. I used the following code:

mDatePickerField = startDatePickerDi         


        
7条回答
  •  Happy的楠姐
    2020-11-27 06:17

    You can set range with init datePicker method. Example with min value :

    // Calendar
    this.calendar = new GregorianCalendar();
    this.datePicker = (DatePicker) findViewById(R.id.xxxxx);
    
    if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
        // (picker is a DatePicker)
        this.datePicker.setMinDate(this.calendar.getTimeInMillis());
    } else {
        final int minYear = this.calendar.get(Calendar.YEAR);
        final int minMonth = this.calendar.get(Calendar.MONTH);
        final int minDay = this.calendar.get(Calendar.DAY_OF_MONTH);
    
        this.datePicker.init(minYear, minMonth, minDay,
                new OnDateChangedListener() {
    
                    public void onDateChanged(DatePicker view, int year,
                            int month, int day) {
                        Calendar newDate = Calendar.getInstance();
                        newDate.set(year, month, day);
    
                        if (calendar.after(newDate)) {
                            view.init(minYear, minMonth, minDay, this);
                        }
                    }
                });
        Log.w(TAG, "API Level < 11 so not restricting date range...");
    }
    

提交回复
热议问题