カスタムViewで日付セレクタを実装

6414 ワード

良い日付セレクタが見えました.特に1-1 Gradleを記録します.
 //     
    implementation 'com.manu:MDatePicker:1.0.1'

1-2 XMLレイアウト




    

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 |

盗人は使いやすい
文章はとても短くて、道はまだ长くて、皆さん、こんにちは、私は玖玖君で、1人のハンサムで才能と并存する男で、私达は次号でまた会います.