Android DatePickerDialog 只显示年月
来源:互联网 发布:淘宝卖的ios迅雷软件 编辑:程序博客网 时间:2024/06/05 23:59
Android DatePickerDialog 只显示年月
今天写一个日期控件,默认显示年月日,但是我现在只想显示年月,在网上找了一个比较简单容易了理解的方法,分享如下:
先看一个效果图:
处理前: 处理后:
实现的代码:
1. 通过遍历方法查找DatePicker里的子控件:年、月、日
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; }
2.隐藏不想显示的子控件,这里隐藏日控件
final Calendar cal = Calendar.getInstance(); mDialog = new CustomerDatePickerDialog(getContext(), this, cal.get(Calendar.YEAR), cal.get(Calendar.MONTH), cal.get(Calendar.DAY_OF_MONTH)); mDialog.show(); DatePicker dp = findDatePicker((ViewGroup) mDialog.getWindow().getDecorView()); if (dp != null) { ((ViewGroup)((ViewGroup) dp.getChildAt(0)).getChildAt(0)).getChildAt(2).setVisibility(View.GONE); }
如果想隐藏年,把getChildAt(2)改为getChildAt(0)...
3.补充:如果标题栏也想改,需要自定义并实现onDateChanged方法才可实现,代码:
class CustomerDatePickerDialog extends DatePickerDialog { public CustomerDatePickerDialog(Context context, OnDateSetListener callBack, int year, int monthOfYear, int dayOfMonth) { super(context, callBack, year, monthOfYear, dayOfMonth); } @Override public void onDateChanged(DatePicker view, int year, int month, int day) { super.onDateChanged(view, year, month, day); mDialog.setTitle(year + "年" + (month + 1) + "月"); } }
- Android DatePickerDialog 只显示年月
- Android DatePickerDialog 只显示年月
- Android DatePickerDialog 只显示年月
- Android DatePickerDialog 只显示年月
- Android DatePickerDialog 只显示年月
- android中DatePickerDialog只显示年月
- Android的DatePickerDialog只显示年月
- android 修改DatePickerDialog只显示“年月”,去掉“日”显示
- Android日历选择控件DatePickerDialog只显示年月
- DatePickerDialog用法及只显示年月隐藏日
- Android 日期控件只显示年月
- Android日历只显示年月,只显示年
- easyui只显示年月
- datepicker只显示年月
- Android 只有年月没有日的datepickerdialog
- Android只显示月和日的DatePickerDialog
- [Android]只显示月和日的DatePickerDialog
- [Android]只显示月和日的DatePickerDialog
- 新开博客,关于ETL调度
- Mysql load 出现 The used command is not allowed with this MySQL version
- ASP.NET C# 数字格式化输出
- AS3中对String操作的replaceAll方法
- SQL Server 2005安装失败的问题
- Android DatePickerDialog 只显示年月
- 一个小的验证码图片生成程序
- QT的Paint 系统
- Ubuntu 联网后自动运行程序 监测网络连接
- 动态链接库与静态链接库的区别
- 使用aop配置事务-实际应用
- Qt中跟踪QPainter::drawRect的实现
- c3p0详细配置
- 暖宝宝 有危害吗