The use of the PickerView pop-up selection box at the bottom of Android

I hope you can leave a follow and like, and there will be more technology sharing in the future


foreword

This time mainly introduces the use of the bottom pop-up box in Android, using two cases to illustrate, the first is the time selector, and then the selector of the custom bottom pop-up box, and the following explains their usage methods one by one.


1. Time selector

First of all, the xml file used this time is as follows

activity_main.xml

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

    <TextView
        android:id="@+id/tv_pickerview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Hello World!"
        android:textColor="@color/black"
        android:textSize="30sp"/>

   <com.google.android.material.button.MaterialButton
       android:id="@+id/btn"
       android:layout_width="120dp"
       android:layout_height="60dp"
       android:backgroundTint="@color/teal_200"
       app:cornerRadius="10dp"
       android:text="获取日期"
       android:textColor="@color/black"
       android:textSize="20sp"
       />

    <com.google.android.material.button.MaterialButton
        android:id="@+id/btn_1"
        android:layout_width="120dp"
        android:layout_height="60dp"
        android:backgroundTint="@color/teal_200"
        app:cornerRadius="10dp"
        android:text="自定义弹窗"
        android:textColor="@color/black"
        android:textSize="20sp"
        />

</LinearLayout>

 The xml layout file is relatively simple, so I won’t repeat it here. What needs to be said is that the bottom pop-up window does not need to be declared in the layout file, and can be implemented directly using Java code.

The first step is to introduce related dependencies, here is a better open source project on Github

implementation'com.contrarywind:Android-PickerView:4.1.9'

The second step is to write the relevant Java code and related settings

                TimePickerView pvTime = new TimePickerBuilder(MainActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tv_pickerview.setText(getTime(date));
                    }
                })
                        .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                            @Override
                            public void onTimeSelectChanged(Date date) {

                            }
                        })
                        .setType(new boolean[]{true, true, true, true, true, true})
                        .setItemVisibleCount(6)
                        .setLineSpacingMultiplier(2.0f)
                        .isAlphaGradient(true)
                        .build();
                pvTime.show();

private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }

 Then run the program, you can achieve


 2. Custom selector

The relevant Java code is as follows

//自定义底部弹窗
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                OptionsPickerView pvOptions = new OptionsPickerBuilder(MainActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String date = options1Items.get(options1) + " " + options2 + "时 " + options3 + "分";
                        //在此获取选择到的内容
                        tv_pickerview.setText(date);
                    }
                })
                        .setTitleText("选择时间")
                        .setContentTextSize(16)
                        .build();

                pvOptions.setPicker(options1Items, options2Items, options3Items);
                pvOptions.show();
            }
        });

private void initData() {
        options1Items.clear();
        //加入第一个列表数据
        options1Items.add("上午");
        options1Items.add("下午");

        List<String> a = new ArrayList<>();
        List<String> b = new ArrayList<>();
        List<List<String>> c = new ArrayList<>();


        //创建最后一个列表数据(分)
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                a.add("0" + i + "分");
            } else {
                a.add(i + "分");
            }
        }

        //创建第二个列表数据(时),同时将最后一个列表数据加入c,加入次数为第二个列表的大小
        for (int i = 0; i <= 12; i++) {
            if (i < 10) {
                b.add("0" + i + "时");
            } else {
                b.add(i + "时");
            }
            c.add(a);
        }

        //将第二个列表数据加入options2Itmes,加入次数为options1Items的大小
        for (int j = 0; j < options1Items.size(); j++) {
            options2Items.add(b);
        }

        //将第三个列表数据加入options3Items,加入次数为第二个列表数据的长度
        for (int i = 0; i < b.size(); i++) {
            options3Items.add(c);
        }
    }

 The result of the operation is as follows

The complete code is as follows: 

package com.example.pickerviewtest;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.bigkoo.pickerview.builder.OptionsPickerBuilder;
import com.bigkoo.pickerview.builder.TimePickerBuilder;
import com.bigkoo.pickerview.listener.OnOptionsSelectListener;
import com.bigkoo.pickerview.listener.OnTimeSelectChangeListener;
import com.bigkoo.pickerview.listener.OnTimeSelectListener;
import com.bigkoo.pickerview.view.OptionsPickerView;
import com.bigkoo.pickerview.view.TimePickerView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

public class MainActivity extends AppCompatActivity {

    TextView tv_pickerview;
    Button btn;
    Button btn_1;

    private final List<String> options1Items = new ArrayList<>();
    private final List<List<String>> options2Items = new ArrayList<>();
    private final List<List<List<String>>> options3Items = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        tv_pickerview = findViewById(R.id.tv_pickerview);
        btn = findViewById(R.id.btn);
        btn_1 = findViewById(R.id.btn_1);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                TimePickerView pvTime = new TimePickerBuilder(MainActivity.this, new OnTimeSelectListener() {
                    @Override
                    public void onTimeSelect(Date date, View v) {
                        tv_pickerview.setText(getTime(date));
                    }
                })
                        .setTimeSelectChangeListener(new OnTimeSelectChangeListener() {
                            @Override
                            public void onTimeSelectChanged(Date date) {

                            }
                        })
                        .setType(new boolean[]{true, true, true, true, true, true})
                        .setItemVisibleCount(6)
                        .setLineSpacingMultiplier(2.0f)
                        .isAlphaGradient(true)
                        .build();
                pvTime.show();
            }
        });


        //自定义底部弹窗
        btn_1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                initData();
                OptionsPickerView pvOptions = new OptionsPickerBuilder(MainActivity.this, new OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        String date = options1Items.get(options1) + " " + options2 + "时 " + options3 + "分";
                        //在此获取选择到的内容
                        tv_pickerview.setText(date);
                    }
                })
                        .setTitleText("选择时间")
                        .setContentTextSize(16)
                        .build();

                pvOptions.setPicker(options1Items, options2Items, options3Items);
                pvOptions.show();
            }
        });
    }

    private String getTime(Date date) {
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        return format.format(date);
    }


    private void initData() {
        options1Items.clear();
        //加入第一个列表数据
        options1Items.add("上午");
        options1Items.add("下午");

        List<String> a = new ArrayList<>();
        List<String> b = new ArrayList<>();
        List<List<String>> c = new ArrayList<>();


        //创建最后一个列表数据(分)
        for (int i = 0; i < 60; i++) {
            if (i < 10) {
                a.add("0" + i + "分");
            } else {
                a.add(i + "分");
            }
        }

        //创建第二个列表数据(时),同时将最后一个列表数据加入c,加入次数为第二个列表的大小
        for (int i = 0; i <= 12; i++) {
            if (i < 10) {
                b.add("0" + i + "时");
            } else {
                b.add(i + "时");
            }
            c.add(a);
        }

        //将第二个列表数据加入options2Itmes,加入次数为options1Items的大小
        for (int j = 0; j < options1Items.size(); j++) {
            options2Items.add(b);
        }

        //将第三个列表数据加入options3Items,加入次数为第二个列表数据的长度
        for (int i = 0; i < b.size(); i++) {
            options3Items.add(c);
        }
    }

}

Summarize

This section introduces the specific implementation of these two bottom pop-up windows. It is relatively easy to implement using third-party open source libraries, so I won’t go into details here.

I hope everyone will support it, give it a thumbs up! ! !

Guess you like

Origin blog.csdn.net/m0_58941767/article/details/126872477