Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
509 views
in Technique[技术] by (71.8m points)

android - Still able to select disabled dates in date picker

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

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

This is working fine and the past dates in date picker looks disabled. But I can still click on a previous date and select it. How to not let that happen ? below is the screenshot of my date picker :-

enter image description here

And here is my code where I am disabling the past dates :-

@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;
    }
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...