2

我正在創建一個DatePickerDialog,就像它在the documentation中完成一樣。然而,我注意到CalendarView的標題(即「2012年12月」,就像今天的標題)在Spinner中設置年份時不會立即改變。我發現週數已正確更改,並且我可以根據onSelectedDayChange回調以適當的日期(月,月,日,年,星期)在對話框中設置標題。此外,如果月份在Spinner中更改,則CalendarView會立即更新。這包括,如果在更改月份之前更改年份,則會正確顯示所選年份。如果CalendarView滾動到其他月份,也會調整爲顯示正確的一年。Android DatePickerDialog CalendarView的標題不一致

這似乎意味着CalendarView根本不會在日期設置時重新繪製標題(可能是優化?)。我在做別的事嗎?有針對這個的解決方法嗎?或者它是實施中的錯誤?

這裏是我的代碼:

public class DatePickerFragment extends DialogFragment 
    implements DatePickerDialog.OnDateSetListener { 
    /** Name of the date stored in a {@link Bundle} */ 
    public static final String KEY_DATE = "key.DatePickerFragment.DATE"; 

    @Override 
    public Dialog onCreateDialog(Bundle icicle) { 
     final Bundle arguments = getArguments(); 
     Time date = arguments == null 
       ? TimeMachine.getTimeFromArray(getArguments().getIntArray(KEY_DATE)) 
       : null; 

     if (date == null) 
      date = TimeMachine.getToday(); 

     _dialog_window = new DatePickerDialog(getActivity(), this, date.year, date.month, date.monthDay); 
     final CalendarView calendar_view = _dialog_window.getDatePicker().getCalendarView(); 
     calendar_view.setOnDateChangeListener(
       new CalendarView.OnDateChangeListener() { 
        @Override 
        public void onSelectedDayChange(CalendarView _, int year, int month, int day) { 
         updateTitle(TimeMachine.getTimeFromArray(new int[]{ year, month, day })); 
        } 
       } 
     ); 

     // Sets the title 
     updateTitle(date); 

     // Create a new instance of DatePickerDialog and return it 
     return _dialog_window; 
    } 

    @Override 
    public void onDateSet(DatePicker _, int year, int month, int day) { 
     final Time date = new Time(); 
     date.set(day, month, year); 
    } 

    private void updateTitle(Time date) {   
     _dialog_window.setTitle(date.format(" %A, %B %e, %Y")); 
    } 

    /** The Dialog window */ 
    private DatePickerDialog _dialog_window; 
} 

回答

0

看到在目前還沒有其他任何別的答案輩出,我解決此問題得到了由不上DatePicker使CalendarView,而是作出一個自定義DialogFragment那有一個數值日期選擇器和一個單獨的CalendarView(實際上我自己的重新實現基於真實的CalendarView來允許調整它的顯示和Otto事件總線)。然後我可以確保每次設置數字日期選擇器時都設置了CalendarView