我想根据FROM和TO日期限制datepicker条目。下面的代码工作得很好但在UI中,当禁用日期和不可点击时,可以选择禁用日期。
以下是代码:
if (v == fromDateButton || v == toDateButton) {
final Calendar now = GregorianCalendar.getInstance();
mYear = now.get(Calendar.YEAR);
mMonth = now.get(Calendar.MONTH);
mDay = now.get(Calendar.DAY_OF_MONTH);
DatePickerDialog dpd = new DatePickerDialog(this,
new DatePickerDialog.OnDateSetListener() {
@Override
public void onDateSet(DatePicker view, int year,
int monthOfYear, int dayOfMonth) {
String day = "", month = "";
monthOfYear = (monthOfYear + 1); // Calendar returns 1st month as 0
if (monthOfYear <= 9)
month = "0" + monthOfYear;
else month = monthOfYear + "";
if (dayOfMonth <= 9)
day = "0" + (dayOfMonth);
else day = (dayOfMonth) + "";
if (v == fromDateButton)
fromDateField.setText(day + "/"
+ month + "/" + year);
else toDateField.setText(day + "/"
+ month + "/" + year);
// Enable todate field
toDateButton.setEnabled(true);
}
}, mYear, mMonth, mDay);
if (!toDateButton.isEnabled()) {
dpd.getDatePicker().setMaxDate(new Date().getTime());
}
if (v == toDateButton) {
// if (fromDateField.getText() != null && fromDateField.getText() != "") {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date;
try {
date = df.parse(fromDateField.getText().toString());
dpd.getDatePicker().setMinDate(date.getTime());
dpd.getDatePicker().setMaxDate(new Date().getTime());
} catch (ParseException e) {
e.printStackTrace();
}
// }
}
if (v == fromDateButton) {
// if (toDateField.getText() != null && toDateField.getText() != "") {
DateFormat df = new SimpleDateFormat("dd/MM/yyyy");
Date date;
try {
date = df.parse(toDateField.getText().toString());
dpd.getDatePicker().setMaxDate(date.getTime());
} catch (ParseException e) {
e.printStackTrace();
}
// }
}
dpd.show();
}
以下是可以从中选择禁用字段的选择器
知道为什么会这样吗?我只需要限制用户选择禁用日期。