美文网首页高级UI
自定义View实现一个日期选择器

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

作者: 玖玖君 | 来源:发表于2019-12-12 10:44 被阅读0次

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

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

1-2XML布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    xmlns:tools="http://schemas.android.com/tools"
    tools:context=".fragment.TwoFragment">


    <Button
        android:id="@+id/btnDateBottom"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:onClick="btnClickDateBottoms"
        android:text="Date Bottom"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <Button
        android:id="@+id/btnDateCenter"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:onClick="btnClickDateCenter"
        android:text="DATE CENTER"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnDateBottom" />

    <com.manu.mdatepicker.MPickerView
        android:id="@+id/mPickerView"
        android:layout_width="wrap_content"
        android:layout_height="160dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/btnDateCenter" />

</android.support.constraint.ConstraintLayout>

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<String> 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实现一个日期选择器

    本文链接:https://www.haomeiwen.com/subject/ctbzgctx.html