开发者

Android date popup, range

开发者 https://www.devze.com 2023-03-17 02:22 出处:网络
I have a date picker that allows a user to select a date, by default this lets you change the year higher and higher to infinite or something, but I want to confine the user to a maximum year. Such as

I have a date picker that allows a user to select a date, by default this lets you change the year higher and higher to infinite or something, but I want to confine the user to a maximum year. Such as only 2 years from now.

new DatePickerDialog(this, mDateSetListener, mYear, mMonth, mDay)

How do I set the max date within that function? I'm sure I can do a "if date > 2014 , then show a toast message and show the datepicker again开发者_StackOverflow until they select a proper date" but I would prefer to just not let the date picker function go above a certain year.

Insight appreciated!


Add a OnDateChangedListener to DatePicker Object and then override the

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

//Do the check here for the year

}

Hope this helps


Create a datepicker and add that view to your dialog layout, If you want any custom behaviour then you need a custom date picker, override the onDateChanged() to behave as per your need.

If just you need is to restrict the maximum date of the date picker, use setMaxDate(long maxDate) for the datepicker.

you can use getDatePicker() on the dialog to get the instance of the it inside the dialog.

refer http://developer.android.com/reference/android/app/DatePickerDialog.html and http://developer.android.com/reference/android/widget/DatePicker.html for more info.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号