棒棒堂CalenderView的DatePicker不会调用OnDateChanged()方法棒棒、方法、DatePicker、CalenderView

2023-09-05 11:08:29 作者:渡口有过寒鸦

我正与一个datepicker和发现的Andr​​oid 5.0下它不会调用OnDateChanged()方法在其OnDateChangedListener当它在CalendarView模式下,即使一个新的日期已被选中。如果机器人:datePickerMode =微调中的DatePicker的XML标签设置,在的DatePicker将显示为纺纱厂,它的会拨打OnDateChanged()当选择一个新的日期。在较早版本的Andr​​oid,一个的DatePicker调用OnDateChanged()时在两个CalendarView和纱厂版本被选择一个新的日期。以下是有关code:

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

在我的应用程序,onDateChanged()不会被调用和mDate没有得到改变,如果DatePicker的是棒棒堂下CalendarView模式,但OnDateChanged()不会被调用和mDate不会改变,如果DatePicker的是纱厂模式。在早期版本的Andr​​oid,OnDateChanged()被调用,并mDate得到了的DatePicker的两个版本改变了。

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.

有没有什么办法让一个CalendarView的DatePicker在5.0调用OnDateChanged()?如果做不到这一点,怎么回事我可以检索更改了日期从DatePicker的,当它在CalendarView模式?

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?

推荐答案

我面临着同样的问题,事情是onDateChange()和onTimeSet()监听的DatePicker和TimePicker不叫棒棒糖更新的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.

原因是结点设备,由于时钟应用程序更新时,监听器不工作。

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

解决方法是一次对话解雇,你需要创建你自己的监听器,并使用日期选择器得到一个日历对象设置的值()方法和日历传递给听众。

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.

一个简单的示例code是

A simple sample code is

/**
 * 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;
}