Android datepicker min max date before api level 11

前端 未结 7 552
生来不讨喜
生来不讨喜 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条回答
  •  广开言路
    2020-11-27 06:40

    Edit your DatePicker.java adding the following 3 code excerpts.

    Declare the variables used for limiting the minimum and maximum date.

    // Variables for defining minimum date 
    private int minimumDay;
    private int minimumMonth;
    private int minimumYear;
    
    // Variables for defining maximum date 
    private int maximumDay;
    private int maximumMonth;
    private int maximumYear;
    

    After the constructors, your have to override onDateChanged method.

    // Called every time the user changes DatePicker values
    public void onDateChanged(DatePicker view, int year, int monthOfYear, 
            int dayOfMonth) {
    
        // Test if chosen date is before minimum date
        boolean beforeMinDate = false;
        boolean afterMaxDate = false;
    
        if(year < minimumYear){
            beforeMinDate = true;
        }
        else if(year == minimumYear){
            if(monthOfYear < minimumMonth){
                beforeMinDate = true;
            }
            else if(monthOfYear == minimumMonth){
                if(dayOfMonth < minimumDay){
                    beforeMinDate = true;
                }
            }
        }
    
        // Test if chosen date is after maximum date
        if(!beforeMinDate){
            if(year > maxYear){
                afterMaxDate = true;
            }
            else if(year == maxYear){
                if(monthOfYear > maxMonth){
                    afterMaxDate = true;
                }
                else if(monthOfYear == maxMonth){
                    if(dayOfMonth > maxDay){
                        afterMaxDate = true;
                    }
                }
            }
        }
    
        // If chosen date is before minimum date, update the date and internal
        // calendar to minimum date, else, check similarly fot the maximum
        // date, else, use the valid chosen date.
        if(beforeMinDate)
        {
            mCalendar.set(minimumYear, minimumMonth, minimumDay, 
                    mCalendar.get(Calendar.HOUR_OF_DAY), 
                    mCalendar.get(Calendar.MINUTE));
            updateDate(minimumYear, minimumMonth, minimumDay);
        }
        else if (afterMaxDate)
        {
            mCalendar.set(maximumYear, maximumMonth, maximumDay, 
                    mCalendar.get(Calendar.HOUR_OF_DAY), 
                    mCalendar.get(Calendar.MINUTE));
            updateDate(maximumYear, maximumMonth, maximumDay);
        }
        else
            mCalendar.set(year, monthOfYear, dayOfMonth, 
                    mCalendar.get(Calendar.HOUR_OF_DAY), 
                    mCalendar.get(Calendar.MINUTE));
    }
    

    And now, create setter methods for the minimum and maximum dates.

        // Method to define minimum permitted date for the picker.
        public void setMinimumDate(int minimumDay, int minimumMonth, 
                int minimumYear)
        {
            this.minimumDay = minimumDay;
            this.minimumMonth = minimumMonth;
            this.minimumYear = minimumYear;
        }
    
        //Method to define maximum permitted date for the picker.
        public void setMaximumDate(int maximumDay, int maximumMonth, 
                int maximumYear)
        {
            this.maximumDay = maximumDay;
            this.maximumMonth = maximumMonth;
            this.maximumYear = maximumYear;
        }
    

    Second part, in the activity you call the DatePicker dialog, you need to set the minimum and maximum desired dates. The code below sets the current date as minimum and two years in the future as the maximum date.

        // Define current date as the minimum desired date
        Calendar c = Calendar.getInstance(); 
        int currentDay = c.get(Calendar.DAY_OF_MONTH);
        int currentMonth = c.get(Calendar.MONTH);
        int currentYear = c.get(Calendar.YEAR);
        inicioDateTimePicker.setMinimumDate(currentDay, currentMonth, 
                currentYear);
    
        // Define two years in the future as the maximum desired date
        Calendar c = Calendar.getInstance(); 
        int currentDay = c.get(Calendar.DAY_OF_MONTH);
        int currentMonth = c.get(Calendar.MONTH);
        int currentYear = c.get(Calendar.YEAR);
        inicioDateTimePicker.setMinimumDate(currentDay, currentMonth, 
                currentYear + 2);
    

提交回复
热议问题