我想在android中禁用日期选择器的过去日期。 我可以使用
来做到这一点dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
此工作正常,日期选择器中的过去日期看起来已禁用。但我仍然可以点击之前的日期并选择它。 怎么不让这种情况发生? 下面是我的日期选择器的截图: -
这是我的代码,我禁用过去的日期: -
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
mActivity = new WeakReference<CreateEvent>(
(CreateEvent) getActivity());
DatePickerDialog dialog = new DatePickerDialog(getActivity(), this, year, month, day);
if(callingView==fromDate){
dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
}else if (callingView==toDate){
dialog.getDatePicker().setMinDate(fromD);
}
// Create a new instance of DatePickerDialog and return it
return dialog;
}
答案 0 :(得分:8)
从评论中得知这是Lollipop中的一个错误。所以,以编程方式修复它。
您需要做的就是检查所选日期并设置最小日期。
答案 1 :(得分:4)
是的,这是一个错误,您需要使用java代码检查所选日期。
过去的日期
public static boolean validatePastDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day > currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month > currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year > currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
未来日期
public static boolean validateFutureDate(Context mContext,int day,int month,int year){
final Calendar c = Calendar.getInstance();
int currentYear = c.get(Calendar.YEAR);
int currentMonth = c.get(Calendar.MONTH)+1;
int currentDay = c.get(Calendar.DAY_OF_MONTH);
if (day < currentDay && year == currentYear && month == currentMonth) {
Toast.makeText(mContext, "Please select valid date", Toast.LENGTH_LONG).show();
return false;
} else if (month < currentMonth && year == currentYear) {
Toast.makeText(mContext, "Please select valid month", Toast.LENGTH_LONG).show();
return false;
} else if (year < currentYear) {
Toast.makeText(mContext, "Please select valid year", Toast.LENGTH_LONG).show();
return false;
}
return true;
}
只需在此方法中传递选定的日期月份,并返回布尔值。
答案 2 :(得分:0)
datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
答案 3 :(得分:0)
要解决Android Lollipop的错误,您可以按如下方式验证日期:
int mYear,mMonth,mDay;
Calendar mcurrentDate = Calendar.getInstance();
mYear = mcurrentDate.get(Calendar.YEAR);
mMonth = mcurrentDate.get(Calendar.MONTH);
mDay = mcurrentDate.get(Calendar.DAY_OF_MONTH);
DatePickerDialog mDatePicker = new DatePickerDialog(context, new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker datepicker, int selectedyear, int selectedmonth, int selectedday) {
// TODO Auto-generated method stub
// validate here with condition to avoid to select past dates
if (selectedyear == mYear && (selectedmonth+1) == mMonth+1) {
if(selectedday < mDay){
Toast.makeText(context, "invalid date", Toast.LENGTH_LONG).show();
return;
}
}
textview.setText(selectedday + "/" + (selectedmonth+1) + "/" + selectedyear);
}
}, mYear, mMonth, mDay);
mDatePicker.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
if(!mDatePicker.isShowing()){
mDatePicker.show();
}