DatePicker和TimePicker
DatePicker常用方法
初始化方法:
datePicker.init(year, month, day,
new OnDateChangedListener() //日期变更监听事件
)
getDayOfMonth();
getMonth();
getYear();
//最大最小日期,可用Calendar类的getTimeInMillis()获取
setMinDate(long minDate);
setMaxDate(long maxDate);
updateDate(int year, int monthOfYear, int dayOfMonth);
隐藏DatePicker 中的年月日
private void hideDay(DatePicker datePicker)
{
Class dpClass = datePicker.getClass();
Field field;
//获取当前安卓系统版本
int SDKVersion = Integer.valueOf(android.os.Build.VERSION.SDK);
View view;
try {
//安卓4.0以下叫mDayPicker,以上叫mDaySpinner
if(SDKVersion < android.os.Build.VERSION_CODES.ICE_CREAM_SANDWICH)
field = dpClass.getDeclaredField("mDayPicker");
else
field = dpClass.getDeclaredField("mDaySpinner");
view = (View) field.get(dpClass);
view.setVisibility(View.GONE);
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
}
}
其他的fieldName可通过反射获取
TimePicker同理, DatePickerDialog则先用datePickerDialog.getDatePicker()获取datePicker
隐藏DatePicker中的CalendarView:
void setCalendarViewShown(boolean shown)
//或在xml中写上标签:
android:calendarViewShown="false"
TimePicker
timePicker.setIs24HourView(true); // 设置是否为24小时制
timePicker.setCurrentHour(12); // 设置当前小时
timePicker.setCurrentMinute(30); // 设置当前分钟
timePicker.setOnTimeChangedListener(new OnTimeChangedListener()); //时间改变监听