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

版权声明:本文为博主原创文章,遵循 CC 4.0 by-sa 版权协议,转载请附上原文出处链接和本声明。
本文链接:https://blog.csdn.net/csdnadcode/article/details/38760743

 可分别隐藏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 年月



展开阅读全文

没有更多推荐了,返回首页