DateRangePicker 在今天前一个月设置限制
DateRangePicker set limit one month before today
我是 android 的新手。现在,我想将日历范围设置为从今天起一个月。我试过 this solution 但我仍然不知道如何让它动态化。而且,我正在使用 Material 日期选择器。这是我的代码:
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());
}
});
}
有人可以帮助我吗?
今天获取: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();
我是 android 的新手。现在,我想将日历范围设置为从今天起一个月。我试过 this solution 但我仍然不知道如何让它动态化。而且,我正在使用 Material 日期选择器。这是我的代码:
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());
}
});
}
有人可以帮助我吗?
今天获取: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();