Android Pickerview selector

Posted by vikela on Fri, 03 Apr 2020 23:46:27 +0200

1. overview

I remember that when I was working on the mall project, I needed to realize the three-level linkage between the province and the city in the address selection to facilitate the user to fill in the address quickly. At that time, I used an open-source control called Android wheel, which felt very easy to use. The only trouble was that I needed to organize and analyze the xml files of the province and the city. The idea was very simple, but the code volume was relatively large. I found another component Android pickerview library by chance


First, you need to add a dependency in the build.gradle file:

compile 'com.airsaid.library:pickerview:1.0.3'

However, this library is currently under maintenance. However, the information is not out of date and can be used. In a few days, I will make a summary of the updated library and post it up!

After adding the dependency, resynchronize the project. The following selectors can be used as required:

  • City selection:
  CityPickerView mCityPickerView = new CityPickerView(this);
                // Set whether clicking outside disappears
                // Set wheel font size
                // Set title
                mCityPickerView.setTitle("Choice of city");
                // Set cancel text
                // Set cancel text color
                // Set cancel text size
                // Set OK text
                // Set OK text color
                // Set text size
                // Set head background
                mCityPickerView.setOnCitySelectListener(new OnSimpleCitySelectListener() {
                    public void onCitySelect(String prov, String city, String area) {
                        // Access to provincial, municipal and differentiated development
                        Log.e(TAG, "province: " + prov + " city: " + city + " area: " + area);

                    public void onCitySelect(String str) {
                        // Get together
                        Toast.makeText(MainActivity.this, "The choice is:" + str, Toast.LENGTH_SHORT).show();
  • Time selection:
 //TimePickerView also has the method to set the style above
                TimePickerView mTimePickerView = new TimePickerView(this, TimePickerView.Type.YEAR_MONTH_DAY);
                // Set whether to cycle
                // Set scroll text size
                // Set time optional range (used in combination with setTime method, it must be set in)
                //Calendar calendar = Calendar.getInstance();
                // mTimePickerView.setRange(calendar.get(Calendar.YEAR) - 100, calendar.get(Calendar.YEAR));
                // Set selection time
                // mTimePickerView.setTime(new Date());
                mTimePickerView.setOnTimeSelectListener(new TimePickerView.OnTimeSelectListener() {
                    public void onTimeSelect(Date date) {
                        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd", Locale.CHINA);
                        Toast.makeText(MainActivity.this, format.format(date), Toast.LENGTH_SHORT).show();
  • Option selection:
OptionsPickerView<String> mOptionsPickerView = new OptionsPickerView<>(this);
                final ArrayList<String> list = new ArrayList<>();
                // Setting data
                // Set option units
                //Mpoptionspickerview. Setlabels ("sex");
                mOptionsPickerView.setOnOptionsSelectListener(new OptionsPickerView.OnOptionsSelectListener() {
                    public void onOptionsSelect(int option1, int option2, int option3) {
                        String sex = list.get(option1);
                        Toast.makeText(MainActivity.this, sex, Toast.LENGTH_SHORT).show();

This library, I highly recommend you to use, after all, we are the same age, see the code, I believe have the same feeling, this library is very simple, strongly recommended!!

Further advancement

Topics: Android xml Gradle