我在安卓系统中调用这样的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'
}如何获得此日历的选定日期?我找不到像onDateSet或OnDateSetListener这样的听众
发布于 2019-11-19 09:57:19
当用户确认有效的选择时,只需使用调用的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...
}
});发布于 2020-04-01 20:35:27
对于那些为这一问题而奋斗的人,以及他们的时间戳已经过时的事实,这里是我的工作解决方案。我需要API 23,所以我不能在java.time中使用任何好的Epoch函数。对我来说,关键是意识到我需要做时区偏移的计算。
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
@Override
public void onPositiveButtonClick(Long selectedDate) {
// user has selected a date
// format the date and set the text of the input box to be the selected date
// right now this format is hard-coded, this will change
;
// Get the offset from our timezone and UTC.
TimeZone timeZoneUTC = TimeZone.getDefault();
// It will be negative, so that's the -1
int offsetFromUTC = timeZoneUTC.getOffset(new Date().getTime()) * -1;
// Create a date format, then a date object with our offset
SimpleDateFormat simpleFormat = new SimpleDateFormat("MM/dd/yyyy", Locale.US);
Date date = new Date(selectedDate + offsetFromUTC);
dataEntry.setText(simpleFormat.format(lDate));
}
});
picker.show(myActivity.getSupportFragmentManager(), picker.toString());发布于 2020-05-13 07:54:24
GR Envoy的答案是好的,但我想稍微改变一下。最好将时区设置为世界协调时。
private val outputDateFormat = SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()).apply {
timeZone = TimeZone.getTimeZone("UTC")
}
...
picker.addOnPositiveButtonClickListener {
val text = outputDateFormat.format(it)
}https://stackoverflow.com/questions/58931051
复制相似问题