Material DateRangePicker设置默认范围。

huangapple go评论30阅读模式
英文:

Material DateRangePicker set up a default range

问题

我想在我的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");
    }
});
英文:

I want in my DateRangePicker was a default selected range. How can I get that?

My code now:

            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");
                }
            });

答案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()));

在Kotlin中:

val now = Calendar.getInstance()
val builder = MaterialDatePicker.Builder.dateRangePicker()
builder.setSelection(androidx.core.util.Pair(now.timeInMillis, now.timeInMillis))
英文:

Please use MaterialDatePicker.Builder.setSelection() method. Below code for setting today as the start date and end date:

In Java:

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

In Kotlin

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

huangapple
  • 本文由 发表于 2020年7月31日 20:48:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63192142.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定