英文:
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))
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论