开发者

Android date popup, range

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

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜