PickerView
1、PickerView是什么
仿iOS的PickerView控件,带有3D圆弧效果,并封装了时间选择和选项选择这两种选择器。
WheelView —— 基础控件
带有3D圆弧效果。
支持文字、颜色、大小设置。
支持背景颜色设置。
支持item的分隔线设置。
支持item间距设置。
支持设置是否循环。
OptionsPickerView —— 选项选择器
支持一、二、三级联动数据。
支持一、二、三级不联动数据。
支持自定义布局。
支持自定义标题栏。
支持“省,市,区”等选项的单位(label)显示、隐藏和自定义。
支持dialog 模式显示。
支持自定义设置容器。
支持实时回调监听。
联动数据支持切换Item时,还原为第一项。
TimePickerView —— 时间选择器
支持选择年、月、日的范围。
支持年月日时分秒显示。
支持设置当前默认时间。
支持自定义布局。
支持自定义标题栏。
支持“年,月,日,时,分,秒”等选项的单位(label)显示、隐藏和自定义。
支持dialog 模式显示。
支持自定义设置容器。
支持实时回调监听。
2、使用方式
1.WheelView的使用
1. 依赖
implementation 'com.contrarywind:Android-PickerView:4.1.9'
2. UI中配置
<com.contrarywind.view.WheelView
android:id="@+id/wheel"
android:layout_width="match_parent"
android:layout_height="match_parent">
</com.contrarywind.view.WheelView>
3. 代码
//导包
import android.os.Bundle;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.bigkoo.pickerview.adapter.ArrayWheelAdapter;
import com.contrarywind.listener.OnItemSelectedListener;
import com.contrarywind.view.WheelView;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
private WheelView wheel;
private ArrayWheelAdapter<String> mMyAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initView();
//集合添加数据
final List<String> list = new ArrayList<>();
list.add("我的中国心");
list.add("北国之春");
list.add("鹿港小镇");
//适配器赋值
mMyAdapter = new ArrayWheelAdapter(list);
//赋值
wheel.setAdapter(mMyAdapter);
//监听事件
wheel.setOnItemSelectedListener(new OnItemSelectedListener() {
@Override
public void onItemSelected(int index) {
//吐丝
Toast.makeText(MainActivity.this, "你选择了:"+list.get(index), Toast.LENGTH_SHORT).show();
}
});
}
private void initView() {
wheel = (WheelView) findViewById(R.id.wheel);
}
}
- 效果图
3、联动
- 代码
//数据一
final List<String> options1Items = new ArrayList<>();
options1Items.add("射雕英雄传");
options1Items.add("神雕侠侣");
options1Items.add("鹿鼎记");
//数据二
List<String> options2Items_01 = new ArrayList<>();
options2Items_01.add("郭靖");
options2Items_01.add("黄蓉");
options2Items_01.add("杨铁心");
options2Items_01.add("欧阳克");
//数据三
List<String> options2Items_02 = new ArrayList<>();
options2Items_02.add("小龙女");
options2Items_02.add("杨过");
options2Items_02.add("大雕");
//数据四
List<String> options2Items_03 = new ArrayList<>();
options2Items_03.add("小柜子");
options2Items_03.add("康熙");
options2Items_03.add("康亲王");
options2Items_03.add("鳌拜");
//联动数据
final List<List<String>> options2Items = new ArrayList<>();
options2Items.add(options2Items_01);
options2Items.add(options2Items_02);
options2Items.add(options2Items_03);
//监听事件
OptionsPickerView optionsPickerView = new OptionsPickerBuilder(this, new OnOptionsSelectListener() {
@Override
public void onOptionsSelect(int options1, int options2, int options3, View v) {
//选择监听
String tx = options1Items.get(options1) +":"+ options2Items.get(options1).get(options2);
Toast.makeText(getBaseContext(), "选择了:"+tx, Toast.LENGTH_SHORT).show();
}
}).build();
//绑定显示文字
optionsPickerView.setPicker(options1Items, options2Items);
//显示
optionsPickerView.show();
- 效果图
3、时间
- 代码
//时间选择器 源码里面找事件类型 yyyy-MM-dd
final SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
//new对象
new TimePickerBuilder(this, new OnTimeSelectListener() {
@Override
public void onTimeSelect(Date date, View v) {
//获取时间的类型
String format = simpleDateFormat.format(date);
//吐司
Toast.makeText(MainActivity.this, ""+format, Toast.LENGTH_SHORT).show();
}
}).build().show();//连贯写
- 效果图