我是安卓新手。现在,我想从今天开始设置日历范围仅一个月。我已经尝试过这个解决方案,但我仍然不知道如何使它成为动态的。而且,我正在使用材料日期选择器。这是我的代码:
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());
}
});
}
有人能帮我吗?