MaterialDatePicker get selected dates

。_饼干妹妹 提交于 2019-12-02 12:28:06

问题


I'm calling a MaterialDatePicker like this in Android:

MaterialDatePicker.Builder<Pair<Long, Long>> builder = MaterialDatePicker.Builder.dateRangePicker();

CalendarConstraints.Builder constraintsBuilder = new CalendarConstraints.Builder();
builder.setCalendarConstraints(constraintsBuilder.build());

int dialogTheme = resolveOrThrow(getContext(), R.attr.materialCalendarTheme);
builder.setTheme(dialogTheme);

MaterialDatePicker<?> picker = builder.build();

picker.show(getFragmentManager(), picker.toString());

the library is:

dependencies {
    implementation 'com.google.android.material:material:1.2.0-alpha01'
}

How can I get the selected date of this calendar? I can't find any listener like onDateSet or OnDateSetListener


回答1:


Just use the addOnPositiveButtonClickListener listener called when the user confirms a valid selection:

For a single date picker:

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
      @Override public void onPositiveButtonClick(Long selection) {
        // Do something...
      }
    });

For a range date picker:

MaterialDatePicker<Pair<Long, Long>> pickerRange = builderRange.build();
pickerRange.show(....);

pickerRange.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Pair<Long, Long>>() {
  @Override public void onPositiveButtonClick(Pair<Long,Long> selection) {
   //Do something...
  }
});


来源:https://stackoverflow.com/questions/58931051/materialdatepicker-get-selected-dates

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!