Android中自定义DatePicker
来源:互联网 发布:乔任梁的半途人生知乎 编辑:程序博客网 时间:2024/05/07 01:21
先看一下效果
看这个图很显然就是一个DatePicker和一个TimePicker组合来实现的
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_marginTop="8dp" android:orientation="horizontal"> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="月" android:textColor="@android:color/black" android:textSize="16sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="日" android:textColor="@android:color/black" android:textSize="16sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="时" android:textColor="@android:color/black" android:textSize="16sp" /> <TextView android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:gravity="center" android:text="分" android:textColor="@android:color/black" android:textSize="16sp" /> </LinearLayout> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="horizontal"> <DatePicker android:id="@+id/date_picker" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:calendarViewShown="false" /> <TimePicker android:id="@+id/time_picker" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" /> </LinearLayout></LinearLayout>
接下来就是逻辑代码部分
AlertDialog.Builder builderStart = new AlertDialog.Builder(this); View viewStart = View .inflate(this, R.layout.dialog_date_time, null); final DatePicker datePickerStart = (DatePicker) viewStart .findViewById(R.id.date_picker); final TimePicker timePickerStart = (android.widget.TimePicker) viewStart .findViewById(R.id.time_picker); builderStart.setView(viewStart);
在这里需要特别注意DatePicker控件中的android:calendarViewShown=”false”,这是用来设置是否显示日历的。
接下来最重要的就是需要隐藏年的信息了,有两种方法来实现。
方法一:
((ViewGroup) ((ViewGroup) datePickerStart.getChildAt(0)) .getChildAt(0)).getChildAt(0).setVisibility(View.GONE);
方法二:
if (datePickerStart != null) { try { Field f = datePickerStart.getClass().getDeclaredField("mYearSpinner"); f.setAccessible(true); LinearLayout l = (LinearLayout) f.get(datePickerStart); l.setVisibility(View.GONE); } catch (NoSuchFieldException e) { e.printStackTrace(); } catch (IllegalAccessException e) { e.printStackTrace(); } }
还需要注意一下几点:
1. 我这里用的是DatePicker,如果用的是DatePickerDialog的话需要先找到DatePicker。
2. 这两中方法4.0以上版本和4.0以下版本都有所区别,我这里只写了4.0以上版本的处理方式。
3. 方法一中有缺陷测试机三星S4(I9502)中,日对应的地方显示的是年。
0 0
- Android中自定义DatePicker
- Android 自定义DatePicker / DatePickerDialog
- Android中DatePicker时间选择器
- android中控件DatePicker控件
- Android 开发中DatePicker、TimePicker
- Android中DatePicker的使用
- Android 自定义带两个DatePicker的DatePickerDialog
- android中如何使用DatePicker获取时间
- Android中DatePicker控件用法解析
- Android控件中DatePicker、TimePicker详解
- Android之DatePicker和TimePicker实现以及自定义大小
- Android之DatePicker和TimePicker实现以及自定义大小
- Android之DatePicker和TimePicker实现以及自定义大小
- 自定义的DatePicker
- 自定义时间选择器DatePicker
- 自定义个性的DatePicker
- 自定义datePicker的实现
- datepicker自定义显示年月日
- <学习笔记>https原理及tomcat配置https方法
- 用 OneAPM Cloud Insight 监控 Docker 性能
- MySQL九个操作
- 集合完整
- Mysql 关于乱码
- Android中自定义DatePicker
- 增大mysql的连接数
- 常见MYSQL技巧
- 解决HttpURLConnection请求时传中文参数乱码问题
- 文件管理文件发送或打开
- jquery UI autocomplete获得焦点自动弹出跟随下拉框
- 视频合成,左右画面合成
- httpRuntime
- OpenGL中纹理坐标和顶点坐标表示