我正在創建一個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;
}