/**
* 日期
* themeResId 1-5任意输入(5种不同的时间选择器)
* <p>
* tvTime 设置时间
* <p>
* calendar 创建Calendar
*/
public void showDatePickerDialog(Activity activity, int themeResId, final TextView tvTime, Calendar calendar) {
// 直接创建一个DatePickerDialog对话框实例,并将它显示出来
new DatePickerDialog(activity, themeResId
// 绑定监听器(How the parent is notified that the date is set.)
, new DatePickerDialog.OnDateSetListener() {
private String times;
@Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
// 此处得到选择的时间,可以进行你想要的操作
times = year + "-" + (monthOfYear + 1) + "-" + dayOfMonth;
tvTime.setText(times);
}
}
// 设置初始日期
, calendar.get(Calendar.YEAR)
, calendar.get(Calendar.MONTH)
, calendar.get(Calendar.DAY_OF_MONTH)).show();
//只展示年月
DatePicker dp = findDatePicker((ViewGroup) datePickerDialog.getWindow().getDecorView());
if (dp != null) {
((ViewGroup)((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);
}
}
/**
* 低版本中获取DatePicker
* @param group
* @return
*/
private DatePicker findDatePicker(ViewGroup group) {
if (group != null) {
for (int i = 0, j = group.getChildCount(); i < j; i++) {
View child = group.getChildAt(i);
if (child instanceof DatePicker) {
return (DatePicker) child;
} else if (child instanceof ViewGroup) {
DatePicker result = findDatePicker((ViewGroup) child);
if (result != null) {
return result;
}
}
}
}
return null;
}