Android如何实现年月选择器功能
程序员文章站
2022-06-25 09:27:52
开发过程中,年月的选择功能还是比较常见的,像这种功能点比较常见,要是每次都要自己手动去写,这无疑会耗费比较多的时间与精力,今天给大家介绍一个第三方库,使用该库来完成年月选择器功能。一、效果图二、实现步...
开发过程中,年月的选择功能还是比较常见的,像这种功能点比较常见,要是每次都要自己手动去写,这无疑会耗费比较多的时间与精力,今天给大家介绍一个第三方库,使用该库来完成年月选择器功能。
一、效果图
二、实现步骤:
1、依赖库
implementation 'cn.aigestudio.wheelpicker:wheelpicker:1.1.3'
2、xml布局文件
<?xml version="1.0" encoding="utf-8"?> <android.support.constraint.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" android:layout_width="match_parent" android:layout_height="200dp" android:background="#ffffff"> <textview android:id="@+id/cancel" android:layout_width="60dp" android:layout_height="40dp" android:gravity="center" android:text="取消" android:textcolor="#666666" android:textsize="17sp" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_totopof="parent" /> <textview android:id="@+id/ok" android:layout_width="60dp" android:layout_height="40dp" android:gravity="center" android:text="确定" android:textcolor="#3c76ff" android:textsize="17sp" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="parent" /> <view android:id="@+id/view_line" android:layout_width="match_parent" android:layout_height="1dp" android:background="#e5e5e5" app:layout_constraintleft_toleftof="parent" app:layout_constrainttop_tobottomof="@id/cancel" /> <com.aigestudio.wheelpicker.wheelpicker android:id="@+id/mwheelpicker_1" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginleft="30dp" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_toleftof="parent" app:layout_constraintright_toleftof="@id/mwheelpicker_2" app:layout_constrainttop_tobottomof="@id/view_line" app:wheel_atmospheric="true" app:wheel_curtain_color="#1886f7" app:wheel_curved="true" app:wheel_cyclic="true" app:wheel_indicator_color="#e5e5e5" app:wheel_item_text_color="#919191" app:wheel_item_text_size="23sp" app:wheel_selected_item_text_color="#000000" /> <com.aigestudio.wheelpicker.wheelpicker android:id="@+id/mwheelpicker_2" android:layout_width="0dp" android:layout_height="0dp" android:layout_marginright="30dp" app:layout_constraintbottom_tobottomof="parent" app:layout_constraintleft_torightof="@id/mwheelpicker_1" app:layout_constraintright_torightof="parent" app:layout_constrainttop_totopof="@id/mwheelpicker_1" app:wheel_atmospheric="true" app:wheel_curtain_color="#1886f7" app:wheel_curved="true" app:wheel_cyclic="true" app:wheel_indicator_color="#e5e5e5" app:wheel_indicator_size="24sp" app:wheel_item_text_color="#919191" app:wheel_item_text_size="23sp" app:wheel_selected_item_text_color="#000000" /> </android.support.constraint.constraintlayout>
3、添加数据
list<string> ceoyear = new arraylist<>(); list<string> ceomonth = new arraylist<>(); for (int i = 2000; i < 2051; i++) { ceoyear.add(i + ""); } for (int i = 1; i < 13; i++) { ceomonth.add(i + ""); }
4、设置选择器弹出框
/** * @desc : 两个滚动器 **/ private void showtwowheelpicker(context context, final list<string> data1, final list<string> data2, final twowheellistener mtwowheellistener) { final dialog dialog = getdialog(context); window window = dialog.getwindow(); window.setgravity(gravity.bottom); window.setlayout(viewgroup.layoutparams.match_parent, viewgroup.layoutparams.wrap_content); window.setcontentview(r.layout.fragment_sami); final wheelpicker wv1 = window.findviewbyid(r.id.mwheelpicker_1); final wheelpicker wv2 = window.findviewbyid(r.id.mwheelpicker_2); wv1.setdata(data1); wv2.setdata(data2); //取消 window.findviewbyid(r.id.cancel).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dialog.dismiss(); } }); //确定 window.findviewbyid(r.id.ok).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { dialog.dismiss(); if (mtwowheellistener != null) { mtwowheellistener.onok(data1.get(wv1.getcurrentitemposition()), data2.get(wv2.getcurrentitemposition())); } } }); } private dialog getdialog(context context) { return new alertdialog.builder(context, r.style.roundcornerdialog).setcancelable(false).show(); } private twowheellistener mtwowheellistener = null; public static interface twowheellistener { void onok(string str1, string str2); }
5、设置弹出框dialog样式
<!--圆角的dialog样式--> <style name="roundcornerdialog" parent="@android:style/theme.dialog"> <item name="android:windowframe">@null</item> <item name="android:windowisfloating">true</item> <item name="android:windowistranslucent">true</item> <item name="android:windownotitle">true</item> <item name="android:background">@android:color/transparent</item> <item name="android:windowbackground">@android:color/transparent</item> <item name="android:backgrounddimenabled">true</item> <item name="android:backgrounddimamount">0.6</item> </style>
6、设置点击事件弹出
findviewbyid(r.id.btn).setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { showtwowheelpicker(appbarlayoutactivity.this, ceoyear, ceomonth, new twowheellistener() { @override public void onok(string str1, string str2) { toast.maketext(appbarlayoutactivity.this, str1 + "年" + str2 + "日", toast.length_short).show(); } }); } });
四、总结
这个第三方库我这里只是做了简单的介绍,还有更多需求的还是去阅读第三方库。
第三方库地址:
https://github.com/aigestudio/wheelpicker
到这里就结束啦。
以上就是android如何实现年月选择器功能的详细内容,更多关于android实现年月选择器功能的资料请关注其它相关文章!
上一篇: Java实时获取基金收益项目源码分享