一、优点
1.可扩展性很强
2.三级联动
3.单项选择
4.条件 选择
二、项目下载地址demo及文档:传送门在这哦
三、效果图
四、使用步骤
1.添加Jcenter仓库 Gradle依赖:
compile 'com.contrarywind:AndroID-PickerVIEw:3.2.7'
2.在Activity中添加如下代码:
默认的时间选择器
//时间选择器TimePickerVIEw pvTime = new TimePickerVIEw.Builder(this, new TimePickerVIEw.OnTimeSelectListener() { @OverrIDe public voID onTimeSelect(Date date,VIEw v) {//选中事件回调 tvTime.setText(getTime(date)); } }) .build(); pvTime.setDate(Calendar.getInstance());//注:根据需求来决定是否使用该方法(一般是精确到秒的情况),此项可以在d出选择器的时候重新设置当前时间,避免在初始化之后由于时间已经设定,导致选中时间与当前时间不匹配的问题。 pvTime.show(); ————————————————
例子 选择出生年月日
private voID initTimePicker1() {//选择出生年月日 //控制时间范围(如果不设置范围,则使用默认时间1900-2100年,此段代码可注释) //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11 Date curDate = new Date(System.currentTimeMillis());//获取当前时间 SimpleDateFormat formatter_year = new SimpleDateFormat("yyyy "); String year_str = formatter_year.format(curDate); int year_int = (int) Double.parseDouble(year_str); SimpleDateFormat formatter_mouth = new SimpleDateFormat("MM "); String mouth_str = formatter_mouth.format(curDate); int mouth_int = (int) Double.parseDouble(mouth_str); SimpleDateFormat formatter_day = new SimpleDateFormat("dd "); String day_str = formatter_day.format(curDate); int day_int = (int) Double.parseDouble(day_str); Calendar selectedDate = Calendar.getInstance();//系统当前时间 Calendar startDate = Calendar.getInstance(); startDate.set(1900, 0, 1); Calendar endDate = Calendar.getInstance(); endDate.set(year_int, mouth_int - 1, day_int); //时间选择器 pvTime1 = new TimePickerVIEw.Builder(this, new TimePickerVIEw.OnTimeSelectListener() { @OverrIDe public voID onTimeSelect(Date date, VIEw v) {//选中事件回调 // 这里回调过来的v,就是show()方法里面所添加的 VIEw 参数,如果show的时候没有添加参数,v则为null /*btn_Time.setText(getTime(date));*/ textvIEw1.setText(getTime(date)); } }) .setType(new boolean[]{true, true, true, false, false, false}) //年月日时分秒 的显示与否,不设置则默认全部显示 .setLabel("年", "月", "日", "", "", "")//默认设置为年月日时分秒 .isCenterLabel(false) .setdivIDercolor(color.RED) .setTextcolorCenter(color.RED)//设置选中项的颜色 .setTextcolorOut(color.BLUE)//设置没有被选中项的颜色 .setContentSize(21) .setDate(selectedDate) .setlinespacingMultiplIEr(1.2f) .settextxOffset(-10, 0,10, 0, 0, 0)//设置X轴倾斜角度[ -90 , 90°] .setRangDate(startDate, endDate)// .setBackgroundID(0x00FFFFFF) //设置外部遮罩颜色 .setDecorVIEw(null) .build(); } private String getTime(Date date) {//可根据需要自行截取数据显示// SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"); SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd"); return format.format(date); }
自定义布局
private voID initCustomOptionPicker() {//条件选择器初始化,自定义布局 /** * @description * * 注意事项: * 自定义布局中,ID为 optionspicker 或者 timepicker 的布局以及其子控件必须要有,否则会报空指针。 * 具体可参考demo 里面的两个自定义layout布局。 */ pvCustomOptions = new OptionsPickerVIEw.Builder(this, new OptionsPickerVIEw.OnoptionsSelectListener() { @OverrIDe public voID onoptionsSelect(int options1, int option2, int options3, VIEw v) { //返回的分别是三个级别的选中位置 String tx = cardItem.get(options1).getPickerVIEwText(); btn_CustomOptions.setText(tx); } }) .setLayoutRes(R.layout.pickervIEw_custom_options, new CustomListener() { @OverrIDe public voID customLayout(VIEw v) { final TextVIEw tvsubmit = (TextVIEw) v.findVIEwByID(R.ID.tv_finish); final TextVIEw tvAdd = (TextVIEw) v.findVIEwByID(R.ID.tv_add); ImageVIEw ivCancel = (ImageVIEw) v.findVIEwByID(R.ID.iv_cancel); tvsubmit.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { pvCustomOptions.returnData(); pvCustomOptions.dismiss(); } }); ivCancel.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { pvCustomOptions.dismiss(); } }); tvAdd.setonClickListener(new VIEw.OnClickListener() { @OverrIDe public voID onClick(VIEw v) { getCardData(); pvCustomOptions.setPicker(cardItem); } }); } }) .isDialog(true) .build(); pvCustomOptions.setPicker(cardItem);//添加数据 }
三级联动
pvOptions = new OptionsPickerVIEw.Builder(this, new OptionsPickerVIEw.OnoptionsSelectListener() { @OverrIDe public voID onoptionsSelect(int options1, int options2, int options3, VIEw v) { //返回的分别是三个级别的选中位置 String tx = options1Items.get(options1).getPickerVIEwText() + options2Items.get(options1).get(options2) /* + options3Items.get(options1).get(options2).get(options3).getPickerVIEwText()*/; btn_Options.setText(tx); }}) .setTitleText("城市选择") .setContentTextSize(20)//设置滚轮文字大小 .setdivIDercolor(color.LTGRAY)//设置分割线的颜色 .setSelectoptions(0, 1)//默认选中项 .setBgcolor(color.BLACK) .setTitleBgcolor(color.DKGRAY) .setTitlecolor(color.LTGRAY) .setCancelcolor(color.YELLOW) .setsubmitcolor(color.YELLOW) .setTextcolorCenter(color.LTGRAY) .isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。 .setLabels("省", "市", "区") .setBackgroundID(0x66000000) //设置外部遮罩颜色 .build(); //pvOptions.setSelectoptions(1,1);/*pvOptions.setPicker(options1Items);//一级选择器*/pvOptions.setPicker(options1Items, options2Items);//二级选择器/*pvOptions.setPicker(options1Items, options2Items,options3Items);//三级选择器*/
时间选择器的属性大全
.setType(new boolean[]{true, true, true, false, false, false}) //年月日时分秒 的显示与否,不设置则默认全部显示.setLabel("年", "月", "日", "", "", "")//默认设置为年月日时分秒.setsubmitText("确定")//确定按钮文字.setCancelText("取消")//取消按钮文字.setTitleText("请选择")//标题.setSubCalSize(18)//确定和取消文字大小.setTitleSize(20)//标题文字大小.setTitlecolor(color.GREEN)//标题文字颜色.setsubmitcolor(color.GREEN)//确定按钮文字颜色.setCancelcolor(color.GREEN)//取消按钮文字颜色.setTitleBgcolor(0xFF333333)//标题背景颜色 Night mode.setBgcolor(0xFF000000)//滚轮背景颜色 Night mode.setoutSIDeCancelable(false)//点击屏幕,点在控件外部范围时,是否取消显示.isCyclic(false)//是否循环滚动.isCenterLabel(false) //是否只显示中间选中项的label文字,false则每项item全部都带有label。.setdivIDercolor(color.YELLOW)//设置分割线的颜色.setTextcolorCenter(color.RED)//设置选中项的颜色.setTextcolorOut(color.BLUE)//设置没有被选中项的颜色.setContentSize(21)//滚轮文字大小.setDate(selectedDate)//// 如果不设置的话,默认是系统时间*/.setlinespacingMultiplIEr(1.2f)//设置两横线之间的间隔倍数.settextxOffset(-10, 0, 10, 0, 0, 0)//设置X轴倾斜角度[ -90 , 90°].setRangDate(startDate, endDate)////起始终止年月日设定.setBackgroundID(0x00FFFFFF) //设置外部遮罩颜色.setDecorVIEw(null)//设置要将pickervIEw显示到的容器ID 必须是vIEwgroup.isDialog(false)//是否显示为对话框样式
总结
以上是内存溢出为你收集整理的Android 时间选择器 PickerView,的详细使用全部内容,希望文章能够帮你解决Android 时间选择器 PickerView,的详细使用所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)