首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MaterialDatePicker获得选定日期

MaterialDatePicker获得选定日期
EN

Stack Overflow用户
提问于 2019-11-19 09:50:28
回答 7查看 17.4K关注 0票数 12

我在安卓系统中调用这样的MaterialDatePicker:

代码语言:javascript
复制
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());

图书馆是:

代码语言:javascript
复制
dependencies {
    implementation 'com.google.android.material:material:1.2.0-alpha01'
}

如何获得此日历的选定日期?我找不到像onDateSetOnDateSetListener这样的听众

EN

回答 7

Stack Overflow用户

回答已采纳

发布于 2019-11-19 09:57:19

当用户确认有效的选择时,只需使用调用的addOnPositiveButtonClickListener侦听器:

对于单日期选择器:

代码语言:javascript
复制
picker.addOnPositiveButtonClickListener(new MaterialPickerOnPositiveButtonClickListener<Long>() {
      @Override public void onPositiveButtonClick(Long selection) {
        // Do something...
        //Calendar calendar = Calendar.getInstance(TimeZone.getTimeZone("UTC"));
        //calendar.setTimeInMillis(selection);   

      }
    });

用于范围日期选择器

代码语言:javascript
复制
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...
  }
});
票数 31
EN

Stack Overflow用户

发布于 2020-04-01 20:35:27

对于那些为这一问题而奋斗的人,以及他们的时间戳已经过时的事实,这里是我的工作解决方案。我需要API 23,所以我不能在java.time中使用任何好的Epoch函数。对我来说,关键是意识到我需要做时区偏移的计算。

代码语言:javascript
复制
        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());
票数 7
EN

Stack Overflow用户

发布于 2020-05-13 07:54:24

GR Envoy的答案是好的,但我想稍微改变一下。最好将时区设置为世界协调时。

代码语言:javascript
复制
private val outputDateFormat = SimpleDateFormat("dd.MM.yyyy", Locale.getDefault()).apply {
    timeZone = TimeZone.getTimeZone("UTC")
}

...
picker.addOnPositiveButtonClickListener {
    val text = outputDateFormat.format(it)
}
票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/58931051

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档