仍然可以selectdateselect器中的禁用date

我想在android中禁用dateselect器的过去date。 我可以通过使用

dialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 

这工作正常,dateselect器中的过去date看起来被禁用。 但是我仍然可以点击以前的date并select它。 如何不让这发生? 下面是我的dateselect器的截图:

在这里输入图像说明

以下是我禁用过去date的代码:

 @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; } 

Solutions Collecting From Web of "仍然可以selectdateselect器中的禁用date"

从评论中得知,这是棒棒糖中的一个错误。 所以,以编程方式解决它。

所有你需要做的是检查选定的date与最小date设置。

是的,这是一个错误,你需要使用java代码检查选定的date。

过去的date

 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; } 

未来date

 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; } 

只需在该方法中传递选定的date月份,年份并返回布尔值。

 datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000); 

要解决Android棒棒糖的错误,您可以validationdate如下:

  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(); }