Android-PickerView系列之介绍与使用篇(一)

声明:本文为博主原创文章,转载请注明出处:小嵩的博客

一、介绍

Android-PickerView是一款仿iOS的PickerView控件,并封装了时间选择和选项选择这两种选择器,详细特性如下:

WheelView —— 基础控件

  • 带有3D圆弧效果。
  • 支持文字、颜色、大小设置。
  • 支持背景颜色设置。
  • 支持item的分隔线设置。
  • 支持item间距设置。
  • 支持设置是否循环。

OptionsPickerView —— 选项选择器

  • 支持一、二、三级联动数据。
  • 支持一、二、三级不联动数据。
  • 支持自定义布局。
  • 支持自定义标题栏。
  • 支持“省,市,区”等选项的单位(label)显示、隐藏和自定义。
  • 支持dialog 模式显示。
  • 支持自定义设置容器。

TimePickerView —— 时间选择器

  • 支持选择年、月、日的范围。
  • 支持年月日时分秒显示。
  • 支持设置当前默认时间。
  • 支持自定义布局。
  • 支持自定义标题栏。
  • 支持“年,月,日,时,分,秒”等选项的单位(label)显示、隐藏和自定义。
  • 支持dialog 模式显示。
  • 支持自定义设置容器。

其中,WheelView 可在XML布局中直接引用:

<com.bigkoo.pickerview.lib.WheelView
            android:id="@+id/wv_options"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
  • 1
  • 2
  • 3
  • 4

TimePickerView —— 时间选择器,支持年月日时分,年月日,年月,时分等格式 ,效果如下:

TimePicker.gif 

OptionsPickerView —— 选项选择器,支持一,二,三级选项选择,并且可以设置是否联动 ,效果如下:



OptionsPicker.gif


二、使用


1.添加 Jcenter 仓库 Gradle 依赖:

compile 'com.contrarywind:Android-PickerView:3.x'
//注:实际引入请把"3.x"替换成具体版本号,最新版本号请以GitHub上面提供的为准
  • 1
  • 2

  GitHub的项目官方地址

2.在你的Activity中添加如下代码:

//时间选择器
 pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date,View v) {//选中事件回调
                tvTime.setText(getTime(date));
            }
        })
             .build();
 pvTime.show();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

或者

//条件选择器
 pvOptions = new  OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                //返回的分别是三个级别的选中位置
                String tx = options1Items.get(options1).getPickerViewText()
                        + options2Items.get(options1).get(option2)
                        + options3Items.get(options1).get(option2).get(options3).getPickerViewText();
                tvOptions.setText(tx);
            }
        }).build();
 pvOptions.setPicker(options1Items, options2Items, options3Items);
 pvOptions.show(); 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13

  简单的两个步骤就能实现功能了,就是这么简单~ 如果默认的样式不符合你的口味,请继续往下看~


三、自定义样式

pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date,View v) {//选中事件回调
                tvTime.setText(getTime(date));
            }
        })
                .setType(TimePickerView.Type.ALL)//默认全部显示
                .setCancelText("Cancel")//取消按钮文字
                .setSubmitText("Sure")//确认按钮文字
                .setContentSize(18)//滚轮文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleText("Title")//标题文字
                .setOutSideCancelable(false)//点击屏幕,点在控件外部范围时,是否取消显示
                .isCyclic(true)//是否循环滚动
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(0xFF666666)//标题背景颜色 Night mode
                .setBgColor(0xFF333333)//滚轮背景颜色 Night mode
                .setRange(calendar.get(Calendar.YEAR) - 20, calendar.get(Calendar.YEAR) + 20)//默认是1900-2100年
                .setDate(new Date())// 默认是系统时间*/
                .setLabel("年","月","日","时","分","秒")
                .build();
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
pvOptions = new  OptionsPickerView.Builder(this, new OptionsPickerView.OnOptionsSelectListener() {
            @Override
            public void onOptionsSelect(int options1, int option2, int options3 ,View v) {
                //返回的分别是三个级别的选中位置
                String tx = options1Items.get(options1).getPickerViewText()
                        + options2Items.get(options1).get(option2)
                        + options3Items.get(options1).get(option2).get(options3).getPickerViewText();
                tvOptions.setText(tx);
            }
        })
                .setSubmitText("确定")//确定按钮文字
                .setCancelText("取消")//取消按钮文字
                .setTitleText("城市选择")//标题
                .setSubCalSize(18)//确定和取消文字大小
                .setTitleSize(20)//标题文字大小
                .setTitleColor(Color.BLACK)//标题文字颜色
                .setSubmitColor(Color.BLUE)//确定按钮文字颜色
                .setCancelColor(Color.BLUE)//取消按钮文字颜色
                .setTitleBgColor(0xFF333333)//标题背景颜色 Night mode
                .setBgColor(0xFF000000)//滚轮背景颜色 Night mode
                .setContentTextSize(18)//滚轮文字大小
                .setLinkage(false)//设置是否联动,默认true
                .setLabels("省", "市", "区")//设置选择的三级单位
                .setCyclic(false, false, false)//循环与否
                .setSelectOptions(1, 1, 1)  //设置默认选中项
                .setOutSideCancelable(false)//点击外部dismiss default true
                .build();

        pvOptions.setPicker(options1Items, options2Items, options3Items);//添加数据源
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29

四、更多

  1. 如果对以上的使用还有疑问的话,可参考Demo代码,请戳我查看demo代码

  2. 如果还是不能满足你产品经理的需求,那么~ 默哀三秒钟… 然后把源代码下载下来自己做改动吧,源代码基本都写了注释了,我也只能帮到这儿了。Github项目地址:Android-PickerView

  3. 关于方法名和参数的详细说明,请参考Wiki:请戳我查看Wiki文档,欢迎提issue,提建议,Pull Request. 
      

猜你喜欢

转载自blog.csdn.net/duoluo9/article/details/78860835