Lollipop CalenderView DatePicker 不调用 OnDateChanged() 方法 [英] Lollipop CalenderView DatePicker doesn't call OnDateChanged() method

查看:22
本文介绍了Lollipop CalenderView DatePicker 不调用 OnDateChanged() 方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 DatePicker,发现在 Android 5.0 下,即使选择了新日期,它也不会在 CalendarView 模式下调用 OnDateChangedListener 中的 OnDateChanged() 方法.如果在 DatePicker 的 xml 标记中设置了 android:datePickerMode="spinner",则 DatePicker 将显示为微调器,并且在选择新日期时调用 OnDateChanged().在早期版本的 Android 中,当在 CalendarView 和 Spinners 版本中选择新日期时,DatePicker 会调用 OnDateChanged().相关代码如下:

I'm working with a DatePicker and finding that under Android 5.0 it will not call the OnDateChanged() method in its OnDateChangedListener when it's in CalendarView mode even though a new date has been selected. If android:datePickerMode="spinner" is set in the DatePicker's xml tag, the DatePicker will appear as spinners and it will call OnDateChanged() when a new date is selected. In earlier versions of Android, a DatePicker calls OnDateChanged() when a new date is selected in both CalendarView and Spinners versions. Here's the relevant code:

@SuppressLint("InflateParams")
View v = getActivity().getLayoutInflater().inflate(R.layout.dialog_date, null);

 DatePicker datePicker = (DatePicker) v.findViewById(R.id.dialog_date_DatePicker);
 datePicker.init(year, month, day, new DatePicker.OnDateChangedListener() {
       @Override
       public void onDateChanged(DatePicker view, int year, int month, int day) {
           //Translate year, month, day into a Date object using a calendar
           mDate = new GregorianCalendar(year, month, day).getTime();
           //Update argument to preserve selected value on rotation
           getArguments().putSerializable(EXTRA_DATE, mDate);
       }
  });

在我的应用程序中,如果 DatePicker 处于 Lollipop 下的 CalendarView 模式,则不会调用 onDateChanged() 并且不会更改 mDate,但是如果 DatePicker 处于 Spinners 模式,则会调用 OnDateChanged() 并且 mDate 会更改.在较早版本的 Android 中,两个版本的 DatePicker 都会调用 OnDateChanged() 并更改 mDate.

In my application, onDateChanged() doesn't get called and mDate doesn't get changed if the DatePicker is in CalendarView mode under Lollipop, but OnDateChanged() does get called and mDate does change if the DatePicker is in Spinners mode. Under earlier versions of Android, OnDateChanged() gets called and mDate gets changed in both versions of the DatePicker.

有什么办法可以让 5.0 中的 CalendarView DatePicker 调用 OnDateChanged()?否则,当它处于 CalendarView 模式时,我还能如何从 DatePicker 检索更改的日期?

Is there any way to get a CalendarView DatePicker in 5.0 to call OnDateChanged()? Failing that, how else can I retrieve a changed date from the DatePicker when it's in CalendarView mode?

推荐答案

我面临同样的问题,问题是 DatePicker 和 TimePicker 的 onDateChange() 和 onTimeSet() 侦听器未在具有棒棒糖更新的 Nexus 设备中调用.

I face the same issue and the thing is onDateChange() and onTimeSet() listeners for DatePicker and TimePicker is not called in Nexus devices with lollipop Update.

>

原因是在nexus设备中,因为时钟应用更新了,监听器不工作了.

The reason is in nexus devices, since the clock app is updated, the listeners are not working.

解决方法是一旦对话框关闭,您需要创建自己的侦听器并使用 datepicker get() 方法在日历对象中设置值并将日历传递给侦听器.

The work around is once the dialog dismiss, you need to create you own listener and set the values in a calendar object using the datepicker get() methods and pass the calendar to the listener.

一个简单的示例代码是

/**
 * Returns the calendar instance once the date and time is set.
 * @return
 */
public Calendar getDateTime() {
    mCalendar.set(datePicker.getYear(),
                  datePicker.getMonth(),
                  datePicker.getDayOfMonth(),
                  timePicker.getCurrentHour(),
                  timePicker.getCurrentMinute());
    return mCalendar;
}

这篇关于Lollipop CalenderView DatePicker 不调用 OnDateChanged() 方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆