Max Date on an Android DatePickerDialog

2019-05-10 08:45发布

I need to set a max year on the DatePickerDialog that I am using but I am unable to find any mechanism of doing that. The documentation mentions getDatePicker () as a public method of the DatePickerDialog. So, I thought, perhaps, that can be used to get the DatePicker and then I could set the max date but I get "Method undefined" error when I try to get the DatePicker.

This is how I am trying to get the DatePicker inside my onCreateDialog.

DatePickerDialog d = new DatePickerDialog(this, dep_dateListener, mYear,
                      mMonth, mDay);
DatePicker dp = d.getDatePicker();
dp.setMaxDate(maxDate);
return d;

Any pointers on how to do this?

Thanks

4条回答
不美不萌又怎样
2楼-- · 2019-05-10 09:18

The getDatePicker() method is available in API Level 11.

查看更多
别忘想泡老子
3楼-- · 2019-05-10 09:24

to make a subclass of DatePickerDialog

override onDateChanged method

see the source code

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import android.app.DatePickerDialog;
import android.content.Context;
import android.widget.DatePicker;

public class MyDatePickerDialog extends DatePickerDialog{
private final int minYear;
private final int minMonth;
private final int minDay;

private int maxYear;
private int maxMonth;
private int maxDay;

private final Calendar mCalendar;
private final SimpleDateFormat formatter;


public MyDatePickerDialog(Context context,OnDateSetListener callBack,int year,int monthOfYear,int dayOfMonth){
    super(context,callBack,year,monthOfYear,dayOfMonth);

    mCalendar = Calendar.getInstance();

    //set up mindate
    minYear=Constant.minYear;
    minMonth=Constant.minMonth;
    minDay=Constant.minDay;

    mCalendar.setTime(new Date());
    //set up maxdate
    mCalendar.add(Calendar.YEAR, Constant.maxBirthdayYearSinceNow);
    maxYear=mCalendar.get(Calendar.YEAR);
    maxMonth=mCalendar.get(Calendar.MONTH);
    maxDay=mCalendar.get(Calendar.DATE);

    //set up init date
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, monthOfYear);
    mCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);

  //set up date display format
    formatter = new SimpleDateFormat("MM/dd/yyyy");
    setTitle(formatter.format(mCalendar.getTime()));
}

@Override
public void onDateChanged(DatePicker view, int year,int month, int day){
    boolean beforeMinDate=false;
    boolean afterMaxDate=false;

    if(year<minYear){
        beforeMinDate=true;
    }
    else if(year==minYear){
        if(month<minMonth){
            beforeMinDate=true;
        }
        else if(month==minMonth){
            if(day<minDay){
                beforeMinDate=true;
            }
        }
    }

    if(!beforeMinDate){
        if(year>maxYear){
            afterMaxDate=true;
        }
        else if(year==maxYear){
            if(month>maxMonth){
                afterMaxDate=true;
            }
            else if(month==maxMonth){
                if(day>maxDay){
                    afterMaxDate=true;
                }
            }
        }
    }

    //need set invalid date to mindate or maxdate
    if(beforeMinDate || afterMaxDate){
        if(beforeMinDate){
            year=minYear;
            month=minMonth;
            day=minDay;
        }
        else{
            year=maxYear;
            month=maxMonth;
            day=maxDay;
        }
        view.updateDate(year,  month,  day);
    }

    //display in view title
    mCalendar.set(Calendar.YEAR, year);
    mCalendar.set(Calendar.MONTH, month);
    mCalendar.set(Calendar.DAY_OF_MONTH, day);
    setTitle(formatter.format(mCalendar.getTime()));
}
}
查看更多
小情绪 Triste *
4楼-- · 2019-05-10 09:29

A better implementation in monodroid:

public DateTime MaxDate { get; set; }
public DateTime MinDate { get; set; }

public override void OnDateChanged(DatePicker view, int year, int month, int day)
    {
        var error = false;

        if (year < MinDate.Year)
        {
            error = true;
            year++;
        }
        else if (year == MinDate.Year)
        {
            if (month < MinDate.Month - 1)
            {
                error = true;
                month++;
            }
            else if (month == MinDate.Month - 1)
            {
                if (day < MinDate.Day)
                {
                    error = true;
                    day++;
                }
            }
        }

        if (!error)
        {
            if (year > MaxDate.Year)
            {
                error = true;
                year--;
            }
            else if (year == MaxDate.Year)
            {
                if (month > MaxDate.Month - 1)
                {
                    error = true;
                    month--;
                }
                else if (month == MaxDate.Month - 1)
                {
                    if (day > MaxDate.Day)
                    {
                        error = true;
                        day--;
                    }
                }
            }
        }

        if (error)
        {
            view.UpdateDate(year, month, day);
        }

    }
查看更多
来,给爷笑一个
5楼-- · 2019-05-10 09:37

setMaxDate() is only useful for new APIs but if you want your application to work with earlier versions then you have to make your own comparator for dates.

For example I just get the local date using Calendar.getInstance() and using the method Date.before(Date) OR Date.after(Date) you can add restrictions for DatePicker.

查看更多
登录 后发表回答