Android自定义日期选择器

先上图:


布局文件:activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:fitsSystemWindows="true"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="56dp"
        android:gravity="center"
        android:background="@color/mainColor"
        android:text="选择器 Picker"
        android:textColor="@color/white"
        android:textSize="20sp" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="日期设置:"
            android:textColor="#656565"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvDate"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:onClick="chooseDateDialog"
            android:text="2016-06-15"
            android:textColor="#656565"
            android:textSize="18sp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="#C9C9C9" />

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:orientation="horizontal">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="center"
            android:text="城市设置:"
            android:textColor="#656565"
            android:textSize="18sp" />

        <TextView
            android:id="@+id/tvCity"
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:layout_alignParentRight="true"
            android:gravity="center"
            android:text="广东-深圳-福田"
            android:textColor="#656565"
            android:textSize="18sp" />
    </RelativeLayout>

    <View
        android:layout_width="match_parent"
        android:layout_height="0.1dp"
        android:background="#C9C9C9" />

</LinearLayout>
接口类  ChooseDateInterface.java

public interface ChooseDateInterface {
    public void sure(int[] newDateArray);
}
日期选择工具类 ChooseDateUtil.java

/**
 * Developer : xiongwenwei@aliyun.com
 * Create Time :2016-6-16 16:25:36
 * Function:日期选择器
 */
public class ChooseDateUtil implements View.OnClickListener, NumberPicker.OnValueChangeListener {

    Context context;
    AlertDialog dialog;
    ChooseDateInterface dateInterface;
    NumberPicker npYear, npMonth, npDay;
    TextView tvCancel, tvSure;
    int[] newDateArray = new int[3];

    public void createDialog(Context context, String[] oldDateArray, ChooseDateInterface dateInterface) {
        this.context = context;
        this.dateInterface = dateInterface;
        newDateArray[0] = Integer.parseInt(oldDateArray[0]);
        newDateArray[1] = Integer.parseInt(oldDateArray[1]);
        newDateArray[2] = Integer.parseInt(oldDateArray[2]);

        dialog = new AlertDialog.Builder(context).create();
        dialog.show();
        Window window = dialog.getWindow();
        window.setContentView(R.layout.dialog_choose_date);
        //初始化控件
        tvCancel = (TextView) window.findViewById(R.id.tvCancel);
        tvSure = (TextView) window.findViewById(R.id.tvSure);
        tvCancel.setOnClickListener(this);
        tvSure.setOnClickListener(this);
        npYear = (NumberPicker) window.findViewById(R.id.npYear);
        npMonth = (NumberPicker) window.findViewById(R.id.npMonth);
        npDay = (NumberPicker) window.findViewById(R.id.npDay);
        //设置选择器最小值、最大值
        npYear.setMinValue(1900);
        npYear.setMaxValue(2100);
        npMonth.setMinValue(1);
        npMonth.setMaxValue(12);
        npDay.setMinValue(1);
        npDay.setMaxValue(31);
        //设置选择器初始值
        npYear.setValue(newDateArray[0]);
        npMonth.setValue(newDateArray[1]);
        npDay.setValue(newDateArray[2]);
        //设置监听
        npYear.setOnValueChangedListener(this);
        npMonth.setOnValueChangedListener(this);
        npDay.setOnValueChangedListener(this);
        //去除分割线
        setNumberPickerDividerColor(npYear);
        setNumberPickerDividerColor(npMonth);
        setNumberPickerDividerColor(npDay);
        //设置字体颜色
        setNumberPickerTextColor(npYear, Color.parseColor("#1BC47A"));
        setNumberPickerTextColor(npMonth, Color.parseColor("#1BC47A"));
        setNumberPickerTextColor(npDay, Color.parseColor("#1BC47A"));
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.tvCancel:
                dialog.dismiss();
                break;
            case R.id.tvSure:
                dialog.dismiss();
                dateInterface.sure(newDateArray);
                break;
        }
    }

    //选择器选择值监听
    @Override
    public void onValueChange(NumberPicker picker, int oldVal, int newVal) {
        switch (picker.getId()) {
            case R.id.npYear:
                newDateArray[0] = newVal;
                npDay.setMaxValue(DateUtil.getNumberOfDays(newDateArray[0], newDateArray[1]));
                break;
            case R.id.npMonth:
                newDateArray[1] = newVal;
                npDay.setMaxValue(DateUtil.getNumberOfDays(newDateArray[0], newDateArray[1]));
                break;
            case R.id.npDay:
                newDateArray[2] = newVal;
                break;
        }
    }

    private void setNumberPickerDividerColor(NumberPicker numberPicker) {
        NumberPicker picker = numberPicker;
        Field[] pickerFields = NumberPicker.class.getDeclaredFields();
        for (Field pf : pickerFields) {
            if (pf.getName().equals("mSelectionDivider")) {
                pf.setAccessible(true);
                try {
                    //设置分割线的颜色值
                    pf.set(picker, new ColorDrawable(context.getResources().getColor(R.color.touming)));// pf.set(picker, new Div)
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                } catch (Resources.NotFoundException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                }
                break;
            }
        }
    }

    public static boolean setNumberPickerTextColor(NumberPicker numberPicker, int color) {
        boolean result = false;
        final int count = numberPicker.getChildCount();
        for (int i = 0; i < count; i++) {
            View child = numberPicker.getChildAt(i);
            if (child instanceof EditText) {
                try {
                    Field selectorWheelPaintField = numberPicker.getClass()
                            .getDeclaredField("mSelectorWheelPaint");
                    selectorWheelPaintField.setAccessible(true);
                    ((Paint) selectorWheelPaintField.get(numberPicker)).setColor(color);
                    ((EditText) child).setTextColor(color);
                    numberPicker.invalidate();
                    result = true;
                } catch (NoSuchFieldException e) {
                    e.printStackTrace();
                } catch (IllegalAccessException e) {
                    e.printStackTrace();
                } catch (IllegalArgumentException e) {
                    e.printStackTrace();
                }
            }
        }
        return result;
    }
}


日期工具类 DateUtil.java

/**
 * Developer : xiongwenwei@aliyun.com
 * Create Time :2016-6-16 16:26:33
 * Function:日期工具类
 */
public class DateUtil {

    //获取某年某月的总天数
    public static int getNumberOfDays(int year, int month) {
        switch (month) {
            case 1:
            case 3:
            case 5:
            case 7:
            case 8:
            case 10:
            case 12:
                return 31;
            case 4:
            case 6:
            case 9:
            case 11:
                return 30;
            case 2:
                if(isRunYear(year)){
                    return 29;
                }else{
                    return 28;
                }
        }
        return 100;
    }

    //判断是否闰年
    private static boolean isRunYear(int year) {
        return (year % 4 != 0) || (year % 100 == 0) && (year % 400 != 0);
    }
}
使用调用:

    //Choose Date 选择日期
    public void chooseDateDialog() {
        final ChooseDateUtil dateUtil = new ChooseDateUtil();
        String[] oldDateArray = tvDate.getText().toString().split("-");
        dateUtil.createDialog(this, oldDateArray, new ChooseDateInterface() {
            @Override
            public void sure(int[] newDateArray) {
                tvDate.setText(newDateArray[0] + "-" + newDateArray[1] + "-" + newDateArray[2]);
            }
        });
    }












评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值