0

我是安卓新手。现在,我想从今天开始设置日历范围仅一个月。我已经尝试过这个解决方案,但我仍然不知道如何使它成为动态的。而且,我正在使用材料日期选择器。这是我的代码:

 private static Calendar getClearedUTC() {
        Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        calendar.clear();
        return calendar;
    }

    private void initSetting() {
        today = MaterialDatePicker.todayInUtcMilliseconds();

        Calendar calendar = getClearedUTC();
        calendar.roll(Calendar.MONTH,  -1);
        monthBefore = calendar.getTimeInMillis();

        todayPair = new Pair<>(today, today);
        mothBeforePair = new Pair<>(monthBefore, monthBefore);
    }

    void onGroupClick(){
        initSetting();

        MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();
        builder.setTitleText("Pilih tanggal");
        builder.setSelection(todayPair);
//        builder.setCalendarConstraints(limitRange().build());
        CalendarConstraints.DateValidator dateValidator = DateValidatorPointForward.from(today);
        final MaterialDatePicker materialDatePicker = builder.build();

        dateBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                materialDatePicker.show(getSupportFragmentManager(), "DATE_PICKER");
            }
        });
        materialDatePicker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener() {
            @Override
            public void onPositiveButtonClick(Object selection) {
                dateTV.setText(materialDatePicker.getHeaderText());
            }
        });
    }

有人能帮我吗?

4

1 回答 1

0

今天获得:Calendar.getInstance()

添加一个月:maxDate.add(Calendar.DAY_OF_MONTH, 30)

减去一个月:maxDate.add(Calendar.DAY_OF_MONTH, -30)

private void enableOneMonthSinceToday() {
    MaterialDatePicker.Builder<Long> builderRange = MaterialDatePicker.Builder.datePicker();
    builderRange.setCalendarConstraints(oneMonthBeforeTodayConstraints().build());
    MaterialDatePicker<Long> pickerRange = builderRange.build();
    pickerRange.show(getSupportFragmentManager(), pickerRange.toString());
}

private CalendarConstraints.Builder oneMonthBeforeTodayConstraints() {
    CalendarConstraints.Builder constraintsBuilderRange = new CalendarConstraints.Builder();

    Calendar maxDate = Calendar.getInstance();
    Calendar minDate = Calendar.getInstance();
    minDate.add(Calendar.DAY_OF_MONTH, -30); // subtracting 30 days

    constraintsBuilderRange.setStart(minDate.getTimeInMillis());
    constraintsBuilderRange.setEnd(maxDate.getTimeInMillis() );

    constraintsBuilderRange.setValidator(new RangeValidator(minDate.getTimeInMillis(), maxDate.getTimeInMillis()));

    return constraintsBuilderRange;

}

并使用最小值和最大值验证范围RangeValidator

static class RangeValidator implements CalendarConstraints.DateValidator {

    long minDate, maxDate;

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

    RangeValidator(Parcel parcel) {
    }

    @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) {

    }

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


}

用法:

enableOneMonthSinceToday();
于 2020-12-15T08:07:53.393 回答