Android DatePicker 显示问题 可分别隐藏年、月或日

 可分别隐藏DatePicker中年、月、日:

			final DatePicker datePicker = (DatePicker) onTouchView
					.findViewById(R.id.date_picker);
			if (datePicker != null) {
				try {
					Field f[] = datePicker.getClass().getDeclaredFields();
					 //隐藏年份
					 for (Field field : f) {
					 if (field.getName().equals("mYearPicker")
					 || field.getName().equals("mYearSpinner")) {
					 field.setAccessible(true);
					 Object yearPicker = new Object();
					 yearPicker = field.get(datePicker);
					 ((View) yearPicker).setVisibility(View.GONE);
					 }
					 }
					// 隐藏月份
					 for (Field field : f) {
					 if (field.getName().equals("mMonthPicker")
					 || field.getName().equals("mMonthSpinner")) {
					 field.setAccessible(true);
					 Object monthPicker = new Object();
					 monthPicker = field.get(datePicker);
					 ((View) monthPicker).setVisibility(View.GONE);
					 }
					 }
					 //隐藏日
					 for (Field field : f) {
					 if (field.getName().equals("mDayPicker")
					 || field.getName().equals("mDaySpinner")) {
					 field.setAccessible(true);
					 Object dayPicker = new Object();
					 dayPicker = field.get(datePicker);
					 ((View) dayPicker).setVisibility(View.GONE);
					 }
					 }
				} catch (SecurityException e) {
					Log.e("ERROR", e.getMessage());
				} catch (IllegalArgumentException e) {
					Log.e("ERROR", e.getMessage());
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}





例如要隐藏年份,只需加入以下代码:

// 隐藏年份
					for (Field field : f) {
						if (field.getName().equals("mYearPicker")
								|| field.getName().equals("mYearSpinner")) {
							field.setAccessible(true);
							Object yearPicker = new Object();
							yearPicker = field.get(datePicker);
							((View) yearPicker).setVisibility(View.GONE);
						}
			



还有一种方式,不过与日期显示格式有关,故不推荐使用,方法如下:

	 ((LinearLayout) ((ViewGroup)
	 datePicker.getChildAt(0)).getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
	 //0 0 0 月日
	 //0 0 1 年日
	 //0 0 2 年月



發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章