自定义View实现一个日期选择器

看到了一个不错的日期选择器,特此记录
1-1Gradle

 //日期选择器
    implementation 'com.manu:MDatePicker:1.0.1'

1-2XML布局





    

1-3Activity代码示例

 @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View inflate = inflater.inflate(R.layout.fragment_two, container, false);
        unbinder = ButterKnife.bind(this, inflate);
        List list = new ArrayList<>();
        list.add("2014");
        list.add("2015");
        list.add("2016");
        list.add("2017");
        list.add("2018");
        list.add("2019");
        list.add("2020");
        list.add("2021");
        list.add("2022");
        list.add("2023");
        list.add("2024");
        list.add("2025");
        list.add("2026");
        list.add("2027");
        list.add("2028");
        list.add("2029");
        list.add("2030");
        list.add("2031");
        list.add("2032");
        list.add("2033");
        list.add("2034");
        list.add("2035");
        if (mMPickerView!=null)
            mMPickerView.setText("年");
            mMPickerView.setData(list);
        DisplayMetrics dm = new DisplayMetrics();
        getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
        Log.i("tag","--density--"+dm.density);
        Log.i("tag","--widthPixels--"+dm.widthPixels);
        Log.i("tag","--heightPixels--"+dm.heightPixels);

        mBtnDateBottom.setOnClickListener(this);
        mBtnDateCenter.setOnClickListener(this::btnClickDateCenter);
        return inflate;
    }


    public void btnClickDateCenter(View view) {
        MDatePickerDialog dialog = new MDatePickerDialog.Builder(getActivity())
                .setCanceledTouchOutside(true)
                .setSupportTime(true)
                .setTwelveHour(true)
                .setCanceledTouchOutside(true)
                .setOnDateResultListener(new MDatePickerDialog.OnDateResultListener() {
                    @Override
                    public void onDateResult(long date) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(date);
                        SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
                        dateFormat.applyPattern("yyyy-MM-dd HH:mm");
                        Toast.makeText(getActivity(), dateFormat.format(new Date(date)), Toast.LENGTH_SHORT).show();
                    }
                })
                .build();
        dialog.show();
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @Override
    public void onClick(View view) {
        MDatePickerDialog dialog = new MDatePickerDialog.Builder(getContext())
                .setCanceledTouchOutside(true)
                .setGravity(Gravity.BOTTOM)
                .setSupportTime(false)
                .setTwelveHour(true)
                .setCanceledTouchOutside(true)//是否支持点击外部取消
                .setOnDateResultListener(new MDatePickerDialog.OnDateResultListener() {
                    @Override
                    public void onDateResult(long date) {
                        Calendar calendar = Calendar.getInstance();
                        calendar.setTimeInMillis(date);
                        SimpleDateFormat dateFormat = (SimpleDateFormat) SimpleDateFormat.getDateInstance();
                        dateFormat.applyPattern("yyyy-MM-dd HH:mm");
                        Toast.makeText(getActivity(), dateFormat.format(new Date(date)), Toast.LENGTH_SHORT).show();
                    }
                })
                .build();
        dialog.show();
    }

1-3各个参数

| 设置 | 对应属性 | 设置方法 | 默认值 |
| --- | --- | --- | --- |
| 标题 | mTitle | setTitle(String mTitle) | 日期选择 |
| 显示位置 | mGravity | setGravity(int mGravity) | Gravity.CENTER |
| 时候支持点击外部区域取消 | isCanceledTouchOutside | setCanceledTouchOutside(boolean canceledTouchOutside) | false |
| 是否支持时间 | isSupportTime | setSupportTime(boolean supportTime) | false |
| 是否支持12小时制 | isTwelveHour | setTwelveHour(boolean twelveHour) | false |

贼啦好用

文章很短,路还漫长,大家好,我是玖玖君,一个帅气与才华并存的男人,我们下期再见。

你可能感兴趣的:(自定义View实现一个日期选择器)