我在我的 Android 活动中使用 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 手机上只显示一个完整的日历。我该怎么做才能使年份表盘可见?