2

我想在我DateRangePicker的默认选择范围内。我怎么能得到那个?

我现在的代码:

            CalendarConstraints.Builder constraintBuilder = new CalendarConstraints.Builder();
            constraintBuilder.setValidator(DateValidatorPointForward.now());

            MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.dateRangePicker();
            builder.setCalendarConstraints(constraintBuilder.build());

            final MaterialDatePicker materialDatePicker = builder.build();

            imageDate.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    materialDatePicker.show(getSupportFragmentManager(), "TAG");
                }
            });
4

1 回答 1

4

请使用MaterialDatePicker.Builder.setSelection()方法。下面的代码用于将今天设置为开始日期和结束日期:

在 Java 中:

Calendar now = Calendar.getInstance()
MaterialDatePicker.Builder builder = MaterialDatePicker.Builder.dateRangePicker();
builder.setSelection(androidx.core.util.Pair(now.getTimeInMillis(), now.getTimeInMillis())

在科特林

 val now = Calendar.getInstance()
 val builder = MaterialDatePicker.Builder.dateRangePicker()
 builder.setSelection(androidx.core.util.Pair(now.timeInMillis, now.timeInMillis))
于 2020-07-31T13:23:37.943 回答