Disable dates in Material date picker in android studio [duplicate]

断了今生、忘了曾经 提交于 2020-12-31 04:52:05

问题


I would love to have am an option where I would select a maxDate and a minDate using calendarConstraints. Just like the normal DatePicker has DatePicker.setMaxDate() and DatePicker.setMinDate() methods, I would love the same methods for materialDatePicker.

I am using materialDatePicker in while developing my application and I am finding it difficult in setting the minDate and maxDate properties for the datePicker opened. I am opening date picker to select a start date and an end date in 2 different text fields for adding a particular period. No, I don't want to select a ranged date. I have looked into this and it does not suit my requirement. I can set a maxDate using

calendarConstraints.setValidator(DateValidatorPointForward.from(startDate)) 

but I can not select a minDate using the materialDatePicker.

code:

     CalendarConstraints.Builder ccb=new CalendarConstraints.Builder();
//        ccb.setEnd(calendar.getTimeInMillis());

//        ccb.setEnd(today);

        MaterialDatePicker.Builder<Pair<Long,Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
        builder.setTitleText("Select Date Range");
        builder.setCalendarConstraints(ccb.build());

回答1:


You can create a custom CalendarConstraints.DateValidator class that takes a minimum and maximum dates, and validates them by overrriding isValid() method which returns a boolean that indicates whether a certain date that will be displayed on the screen is valid or not.

Here is a full example that selects dates from 15/02/2020 to 20/03/2020

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        MaterialDatePicker.Builder<Pair<Long, Long>> builderRange = MaterialDatePicker.Builder.dateRangePicker();
        builderRange.setCalendarConstraints(limitRange().build());
        builderRange.setTitleText("Select Date Range");
        MaterialDatePicker<Pair<Long, Long>> pickerRange = builderRange.build();
        pickerRange.show(getSupportFragmentManager(), pickerRange.toString());

    }



    /*
       Limit selectable Date range
     */
    private CalendarConstraints.Builder limitRange() {

        CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();

        Calendar calendarStart = Calendar.getInstance();
        Calendar calendarEnd = Calendar.getInstance();

        int year = 2020;
        int startMonth = 2;
        int startDate = 15;

        int endMonth = 3;
        int endDate = 20;
        calendarStart.set(year, startMonth - 1, startDate - 1);
        calendarEnd.set(year, endMonth - 1, endDate);

        long minDate = calendarStart.getTimeInMillis();
        long maxDate = calendarEnd.getTimeInMillis();


        constraintsBuilderRange.setStart(minDate);
        constraintsBuilderRange.setEnd(maxDate);
        constraintsBuilderRange.setValidator(new RangeValidator(minDate, maxDate));

        return constraintsBuilderRange;
    }




    static class RangeValidator implements CalendarConstraints.DateValidator {

        long minDate, maxDate;

        RangeValidator(long minDate, long maxDate) {
            this.minDate = minDate;
            this.maxDate = maxDate;
        }

        RangeValidator(Parcel parcel) {
            minDate = parcel.readLong();
            maxDate = parcel.readLong();
        }

        @Override
        public boolean isValid(long date) {
            return !(minDate > date || maxDate < date);
        }

        @Override
        public int describeContents() {
            return 0;
        }

        @Override
        public void writeToParcel(Parcel dest, int flags) {
            dest.writeLong(minDate);
            dest.writeLong(maxDate);
        }

        public static final Parcelable.Creator<RangeValidator> CREATOR = new Parcelable.Creator<RangeValidator>() {

            @Override
            public RangeValidator createFromParcel(Parcel parcel) {
                return new RangeValidator(parcel);
            }

            @Override
            public RangeValidator[] newArray(int size) {
                return new RangeValidator[size];
            }
        };


    }

}



回答2:


You should use the function DateValidatorPointBackward to set maxDate. Somethings like this:

constraintsBuilder.setValidator(
    CompositeDateValidator.allOf(
        listOf(
            DateValidatorPointForward.from(FROM_TIME),
            DateValidatorPointBackward.before(TO_TIME)
        )
    )
)


来源:https://stackoverflow.com/questions/60846772/disable-dates-in-material-date-picker-in-android-studio

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!