Android中自定义DatePicker
16lz
2021-01-24
先看一下效果
看这个图很显然就是一个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)中,日对应的地方显示的是年。
更多相关文章
- Android(安卓)MediaPlayer研究问题
- Android底部导航栏实现(一)之BottomNavigationBar
- Android(安卓)动态获取创建与删除文件权限
- Android查看包名和获取包名
- php、java、android、ios通用的3des方法(推荐)
- 保存Activity状态
- android v7支持包导入问题,Support Error的正确解决方法
- Android(安卓)sdk manager更新 下载API源码
- Android(安卓)UI系列-----CheckBox和RadioButton(1)