Still able to select disabled dates in date picker

后端 未结 4 1931
暖寄归人
暖寄归人 2020-12-16 18:31

I want to disable the past dates of date picker in android. i can do it by using

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


        
相关标签:
4条回答
  • 2020-12-16 19:23

    Got to know from a comment that this is a bug in Lollipop. So, fixed it programatically.

    All you need to do is check the selected date with the min date set.

    0 讨论(0)
  • 2020-12-16 19:27

    To resolve bug for Android Lollipop you can validate date as below:

        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();
        }
    
    0 讨论(0)
  • 2020-12-16 19:30
    datePickerDialog.getDatePicker().setMinDate(System.currentTimeMillis() - 1000);
    
    0 讨论(0)
  • 2020-12-16 19:33

    Yes it is a bug and you need to check the selected date using java code.

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

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

    Just pass the selected date month ,year in this method and will return boolean value.

    0 讨论(0)
提交回复
热议问题