Android DatePickerDialog 上的最大日期

发布于 2024-10-20 09:46:44 字数 470 浏览 8 评论 0原文

我需要在我正在使用的 DatePickerDialog 上设置最大年份,但我找不到任何这样做的机制。文档提到 getDatePicker () 作为 DatePickerDialog 的公共方法。所以,我想,也许,这可以用来获取 DatePicker,然后我可以设置最大日期,但当我尝试获取 DatePicker 时,我收到“方法未定义”错误。

这就是我试图在 onCreateDialog 中获取 DatePicker 的方法。

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

有关如何执行此操作的任何指示?

谢谢

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔嚣张 2024-10-27 09:46:44

getDatePicker() 方法在 API 级别 11 中可用。

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

硪扪都還晓 2024-10-27 09:46:44

要使 DatePickerDialog 的子类

重写 onDateChanged 方法,

请参阅

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()));
}
}

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()));
}
}
仲春光 2024-10-27 09:46:44

setMaxDate() 仅对新 API 有用,但如果您希望应用程序能够与早期版本一起使用,那么您必须创建自己的日期比较器。

例如,我只是使用 Calendar.getInstance() 并使用方法 Date.before(Date) OR Date.after(Date) 获取本地日期code> 您可以为 DatePicker 添加限制。

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.

东走西顾 2024-10-27 09:46:44

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);
        }

    }

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);
        }

    }
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文