我在Android Activity中使用了DatePicker。我只需要向用户显示年份。我用
private void findAndHideField(DatePicker datepicker, String name) {
try {
Field field = DatePicker.class.getDeclaredField(name);
field.setAccessible(true);
View fieldInstance = (View) field.get(datepicker);
fieldInstance.setVisibility(View.GONE);
} catch (Exception e) {
e.printStackTrace();
}
}
然后
findAndHideField(startDatePicker, "mDayPicker");
findAndHideField(startDatePicker, "mMonthPicker");
findAndHideField(startDatePicker, "mDaySpinner");
findAndHideField(startDatePicker, "mMonthSpinner");
代码在2.3.x上工作正常,但在4.1.x手机上,这只显示一个完整的日历。我该怎么办才能使年份表盘可见?
答案 0 :(得分:2)
在XML中添加这些行
android:calendarViewShown="false"
到 DatePicker