在DatePIcker中设置MinDate和MaxDate [英] Set MinDate and MaxDate in DatePIcker
问题描述
创建我正在显示DatePicker的应用程序.现在我想将DatePicker的MinDate设置为前两年,最大日期设置为未来两年.选择应基于当前日期.假设当前Date为2016/11/23,所以datepicker应该在DatePicker中显示直到2014年11月23日的日期,所有日期都应在2014年11月23日之前禁用.当我们单击Datepicker时,光标应位于当前日期.创建的DtaePicker
Creating app in which i am showing DatePicker.Now i want to set MinDate of DatePicker is previous two years and max date future two years only.Selection should be base on current date.Suppose current Date is 23/11/2016 so datepicker should show date till 23/11/2014 in DatePicker all the date should be disabled before the 23/11/2014.And when we click on Datepicker cursor should be on current date.Created DtaePicker
private void showDateDailog() {
final DatePickerDialog datePickerDialog = new DatePickerDialog(mContext, new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker datePicker, int selectedYear, int selectedMonth, int selectedDate) {
year = selectedYear;
month = selectedMonth;
day = selectedDate;
((TextView) findViewById(R.id.textViewTORStartDate)).setText(new StringBuilder().append(day).append("/")
.append(month + 1).append("/").append(year));
}
}, year, month, day);
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis());
datePickerDialog.show();
}
推荐答案
要设置今天之前两年的最小日期和今天之后两年的最大日期,请使用以下代码:
To set the min date two years before and max two years after today use the following code:
Calendar c = Calendar.getInstance();
c.add(Calendar.YEAR, -2); // subtract 2 years from now
datePickerDialog.getDatePicker().setMinDate(c.getTimeInMillis());
c.add(Calendar.YEAR, 4); // add 4 years to min date to have 2 years after now
datePickerDialog.getDatePicker().setMaxDate(c.getTimeInMillis());
这篇关于在DatePIcker中设置MinDate和MaxDate的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!