Android中自定义DatePicker

先看一下效果
自定义的DatePicker
看这个图很显然就是一个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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值