gpt4 book ai didi

android - MaterialDatePicker 获取选择的日期

转载 作者:行者123 更新时间:2023-11-29 16:28:01 28 4
gpt4 key购买 nike

我在 Android 中这样调用 MaterialDatePicker:

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

图书馆是:

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

如何获取此日历的选定日期?我找不到像 onDateSetOnDateSetListener

这样的监听器

最佳答案

只需使用在用户确认有效选择时调用的 addOnPositiveButtonClickListener 监听器:

对于单个日期选择器:

picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override public void onPositiveButtonClick(Long selection) {
// Do something...
//Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
//calendar.setTimeInMillis(selection);

}
});

对于范围日期选择器:

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

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

关于android - MaterialDatePicker 获取选择的日期,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58931051/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com