
年月控制這部分分為控制按鈕,月下拉列表和年Spinner(翻譯為微調(diào)控制器。。。。。。。)。控制按鈕沒(méi)什么好說(shuō)的,無(wú)非就是控制加一減一,注意邊界循環(huán)就可以了。
Spinner的用法很簡(jiǎn)單,看下代碼:

//初始化,只讀,帶邊框










//加上選擇事件監(jiān)聽(tīng)






月的下拉列表看似簡(jiǎn)單實(shí)則不然,因?yàn)槲覀円紤]國(guó)際化的問(wèn)題。


以上是中文環(huán)境和英文環(huán)境下的測(cè)試效果。關(guān)鍵點(diǎn)是:月的信息從Locale中取。以下是關(guān)鍵代碼:
private void initMonth() {
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale);
String[] monthNames = dateFormatSymbols.getMonths();
int month = monthChooser.getSelectionIndex();
if (monthChooser.getItemCount() > 0) {
monthChooser.removeAll();
}
for (int i = 0; i < monthNames.length; i++) {
String name = monthNames[i];
if (name.length() > 0) {
monthChooser.add(name);
}
}
if (month < 0) {
month = 0;
} else if (month >= monthChooser.getItemCount()) {
month = monthChooser.getItemCount() - 1;
}
monthChooser.select(month);
}
DateFormatSymbols dateFormatSymbols = new DateFormatSymbols(locale);
String[] monthNames = dateFormatSymbols.getMonths();
int month = monthChooser.getSelectionIndex();
if (monthChooser.getItemCount() > 0) {
monthChooser.removeAll();
}
for (int i = 0; i < monthNames.length; i++) {
String name = monthNames[i];
if (name.length() > 0) {
monthChooser.add(name);
}
}
if (month < 0) {
month = 0;
} else if (month >= monthChooser.getItemCount()) {
month = monthChooser.getItemCount() - 1;
}
monthChooser.select(month);
}