android开发笔记--时间控件只显示年月不显示日期【如有需要可以更为只显示月日不显示年份】
来源:互联网 发布:龙族js挂机脚本 编辑:程序博客网 时间:2024/06/05 20:54
在2.2系统中DatePicker控件保含了一个LinearLayout子view,而LinearLayout中包含三个NumberPicker控件分别用来显示年月日NumberPicker控件中又包含了三个子view。
因此只要找到显示日的控件,然后将其隐藏掉,就可以了。
在4.0系统中DatePicker中view层次结构与2.2不同,多了一层:
DatePicker包含了一个子view:LinearLayout.
LinearLayout中包含了两个子view:LinearLayout和CalendarView.
子LiearLayout中包含了三个NumberPicker控件
----------------------------重要代码---------------------------------------------------------
因此只要找到显示日的控件,然后将其隐藏掉,就可以了。
if (dp != null) { ((ViewGroup) dp.getChildAt(0)).getChildAt(0).setVisibility(View.GONE); }
在4.0系统中DatePicker中view层次结构与2.2不同,多了一层:
DatePicker包含了一个子view:LinearLayout.
LinearLayout中包含了两个子view:LinearLayout和CalendarView.
子LiearLayout中包含了三个NumberPicker控件
----------------------------重要代码---------------------------------------------------------
<span style="white-space:pre"></span>/** * 自定义年月控件 * * @author xingyimin * */class CustomerDatePickerDialog extends DatePickerDialog {public CustomerDatePickerDialog(Context context,OnDateSetListener callBack, int year, int monthOfYear,int dayOfMonth) {super(context,android.R.style.Theme_Holo_Light_Dialog, callBack, year, monthOfYear, dayOfMonth);}@Overridepublic void onDateChanged(DatePicker view, int year, int month, int day) {dpDialog.setTitle(year + "年" + (month + 1) + "月");}}
/** * 从当前Dialog中查找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;}
<span style="white-space:pre"></span>/** * 日期控件回调函数 */private OnDateSetListener setDateCallBack = new OnDateSetListener() {public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {Calendar calendar = Calendar.getInstance();calendar.set(year, monthOfYear, dayOfMonth);date = new SimpleDateFormat("yyyy-MM-dd").format(calendar.getTime());tvDate.setText(new SimpleDateFormat("yyyy-MM").format(calendar.getTime()));}};
<span style="white-space:pre"></span> final Calendar cal = Calendar.getInstance();DatePickerDialog dpDialog = new CustomerDatePickerDialog(context, setDateCallBack,cal.get(Calendar.YEAR), cal.get(Calendar.MONTH),cal.get(Calendar.DAY_OF_MONTH));dpDialog.setTitle(cal.get(Calendar.YEAR) + "年"+ cal.get(Calendar.MONTH) + "月");dpDialog.show();DatePicker dp = findDatePicker((ViewGroup) dpDialog.getWindow().getDecorView());if (dp != null) {((ViewGroup) dp.getChildAt(0)).getChildAt(1).setVisibility(View.GONE);((ViewGroup) ((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE);}
0 1
- android开发笔记--时间控件只显示年月不显示日期【如有需要可以更为只显示月日不显示年份】
- Android 日期控件只显示年月
- 时间控件只显示年月
- 日期控件DatePicker只显示年月不显示日
- 日期控件DatePicker只显示年月不显示日
- easyui-datebox 日期控件 只显示年月 不显示日
- 只显示年月的日期控件
- easyui时间控件只显示年月
- Android日期控件——只显示年月(通用版)
- 让日期只显示年月
- 日期选择器只显示年月
- Android 编程笔记 只显示年月的时间选择器对话框
- DateTimePicker控件只显示年份
- 日期控件NoDayDatePicker只显示年月不显示日 兼容5.0以上的方法
- Android DatePickerDialog 只显示年月
- Android DatePickerDialog 只显示年月
- Android DatePickerDialog 只显示年月
- Android DatePickerDialog 只显示年月
- java 迭代器
- 有向图的十字链表存储
- jQuery插件之ajaxFileUpload
- Android 设置 横屏 竖屏
- Java基础重点回顾
- android开发笔记--时间控件只显示年月不显示日期【如有需要可以更为只显示月日不显示年份】
- c3p0异常
- MongoDB笔记3-shell collection对象
- SOAP详解
- Android设置虚线、圆角、渐变
- 3.8
- JAVA 整型和字符串互相转换
- 读好书的心得体会
- 人民日报任仲平:凝聚当代中国的价值公约数