仍然可以 select 在日期选择器中禁用日期

Still able to select disabled dates in date picker

我想在 android 中禁用日期选择器的过去日期。 我可以使用

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

这工作正常,日期选择器中的过去日期看起来已禁用。但我仍然可以点击之前的日期并 select 它。 如何不让这种情况发生? 下面是我的日期选择器的截图:-

这是我禁用过去日期的代码:-

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

从评论中了解到这是 Lollipop 中的一个错误。因此,以编程方式修复它。

您需要做的就是检查所选日期是否设置了最短日期。

是的,这是一个错误,您需要使用 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;
}

只要在这个方法中传递选定的日期月份,年份,就会return布尔值。

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

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