如何在 material-component 的 dateRangePicker 上使用 setSelection?
how to use setSelection on dateRangePicker of material-component?
我想在打开 dateRangePicker 时显示选定的日期范围,我知道 setSelected 方法就是为此而我试过它不起作用,或者我做错了什么,请帮助
这是我已经尝试过的
public void openDateRangePicker(View view) {
MaterialDatePicker.Builder builder =
MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
builder.setCalendarConstraints(constraintsBuilder.build());
picker.setStyle(DialogFragment.STYLE_NORMAL, R.style.Custom_MaterialCalendar_Fullscreen);
if(!firstDateStr.isEmpty() || !endDateStr.isEmpty()){
builder.setSelection(selectionDates);
}
picker.show(getSupportFragmentManager(), picker.toString());
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
@Override
public void onPositiveButtonClick(Pair<Long, Long> selection) {
long firstDateLong = selection.first;
Date firstDate=new Date(firstDateLong);
long endDateLong = selection.second;
Date endDate=new Date(endDateLong);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
//format yyyy-MM-dd
firstDateStr = sdf2.format(firstDate);
endDateStr = sdf2.format(endDate);
selectionDates = selection;
selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
tvDates.setText(selectedDatesStr);
tvDates.setTypeface(Typeface.DEFAULT_BOLD);
picker.dismiss();
}
});
}
我在这里做的是保存选择对象,并在下次打开选择器时用它来显示范围,但是它打开时没有任何选择,就像第一次打开一样!
行的顺序很重要,您需要在完成构建器设置后调用 builder.build();
。
String firstDateStr="";
String endDateStr="";
Pair<Long, Long> selectionDates=null;
public void openDateRangePicker() {
MaterialDatePicker.Builder builder =
MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());
// picker.setStyle(DialogFragment.STYLE_NORMAL);
if(selectionDates!=null){
builder.setSelection(selectionDates);
}
MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
@Override
public void onPositiveButtonClick(Pair<Long, Long> selection) {
long firstDateLong = selection.first;
Date firstDate=new Date(firstDateLong);
long endDateLong = selection.second;
Date endDate=new Date(endDateLong);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
//format yyyy-MM-dd
firstDateStr = sdf2.format(firstDate);
endDateStr = sdf2.format(endDate);
selectionDates = selection;
selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
tvDates.setText(selectedDatesStr);
tvDates.setTypeface(Typeface.DEFAULT_BOLD);
picker.dismiss();
}
});
}
我想在打开 dateRangePicker 时显示选定的日期范围,我知道 setSelected 方法就是为此而我试过它不起作用,或者我做错了什么,请帮助
这是我已经尝试过的
public void openDateRangePicker(View view) {
MaterialDatePicker.Builder builder =
MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
builder.setCalendarConstraints(constraintsBuilder.build());
picker.setStyle(DialogFragment.STYLE_NORMAL, R.style.Custom_MaterialCalendar_Fullscreen);
if(!firstDateStr.isEmpty() || !endDateStr.isEmpty()){
builder.setSelection(selectionDates);
}
picker.show(getSupportFragmentManager(), picker.toString());
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
@Override
public void onPositiveButtonClick(Pair<Long, Long> selection) {
long firstDateLong = selection.first;
Date firstDate=new Date(firstDateLong);
long endDateLong = selection.second;
Date endDate=new Date(endDateLong);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
//format yyyy-MM-dd
firstDateStr = sdf2.format(firstDate);
endDateStr = sdf2.format(endDate);
selectionDates = selection;
selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
tvDates.setText(selectedDatesStr);
tvDates.setTypeface(Typeface.DEFAULT_BOLD);
picker.dismiss();
}
});
}
我在这里做的是保存选择对象,并在下次打开选择器时用它来显示范围,但是它打开时没有任何选择,就像第一次打开一样!
行的顺序很重要,您需要在完成构建器设置后调用 builder.build();
。
String firstDateStr="";
String endDateStr="";
Pair<Long, Long> selectionDates=null;
public void openDateRangePicker() {
MaterialDatePicker.Builder builder =
MaterialDatePicker.Builder.dateRangePicker();
CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());
// picker.setStyle(DialogFragment.STYLE_NORMAL);
if(selectionDates!=null){
builder.setSelection(selectionDates);
}
MaterialDatePicker<Pair<Long,Long>> picker = builder.build();
picker.show(getSupportFragmentManager(), picker.toString());
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
@Override
public void onPositiveButtonClick(Pair<Long, Long> selection) {
long firstDateLong = selection.first;
Date firstDate=new Date(firstDateLong);
long endDateLong = selection.second;
Date endDate=new Date(endDateLong);
SimpleDateFormat sdf2 = new SimpleDateFormat("yyyy-MM-dd", Locale.getDefault());
//format yyyy-MM-dd
firstDateStr = sdf2.format(firstDate);
endDateStr = sdf2.format(endDate);
selectionDates = selection;
selectedDatesStr = firstDateStr + " to " + endDateStr ;//+ " (" + (daysBetween + 1) + " days)";
tvDates.setText(selectedDatesStr);
tvDates.setTypeface(Typeface.DEFAULT_BOLD);
picker.dismiss();
}
});
}