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)中,日对应的地方显示的是年。

猜你喜欢

转载自blog.csdn.net/zhong1113/article/details/49249843
今日推荐