WheelPicker自定义时间选择器控件
更新时间:2021年5月31日 15:00 点击:2074
本文实例为大家分享了WheelPicker自定义时间选择器控件的具体代码,供大家参考,具体内容如下
先上图:
使用android自带的DatePicker控件虽然也能实现功能,但样式不能改变。想要实现一些 自定义的样式,就要用到WheelPicker了。
要使用WheelPicker,需要先导入WheelPicker的引用:
1. 在project的build.gradle添加如下代码
allprojects { repositories { jcenter() maven { url "https://jitpack.io" } } }
2. 在Module的build.gradle添加依赖
compile 'com.github.open-android:WheelPicker:v1.0.0'
3.复制如下代码到xml中:
<com.itheima.wheelpicker.WheelPicker android:id="@+id/wheel_date_picker_month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" app:wheel_atmospheric="true" app:wheel_curved="true" app:wheel_cyclic="true" app:wheel_selected_item_position="5" app:wheel_item_text_color="@color/text_white" app:wheel_selected_item_text_color="@color/orange"/>
- wheel_atmospheric : 条目颜色是否执行衔接处理 效果更好
- wheel_curved : 是否是弧形状态显示
- wheel_cyclic : 是否可循环
- wheel_selected_item_position : 默认选中第几个条目
然后使用即可。
页面代码:
package com.example.castedemo.user; import android.app.Activity; import android.os.Bundle; import android.util.Log; import android.widget.DatePicker; import com.example.castedemo.R; import java.util.ArrayList; import java.util.Calendar; import java.util.List; import butterknife.BindView; import butterknife.ButterKnife; import com.example.castedemo.user.bean.DayBean; import com.itheima.wheelpicker.WheelPicker; public class BirthdayActivity extends Activity { private static final String TAG = "BirthdayActivity"; @BindView(R.id.wheel_date_picker_year) WheelPicker wheelDatePickerYear; @BindView(R.id.wheel_date_picker_month) WheelPicker wheelDatePickerMonth; @BindView(R.id.wheel_date_picker_day) WheelPicker wheelDatePickerDay; List<Integer> yearList = new ArrayList<Integer>(); List<Integer> monthList = new ArrayList<Integer>(); int[] dayArr = {31,28,31,30,31,30,31,31,30,31,30,31}; int[] runDayArr = {31,29,31,30,31,30,31,31,30,31,30,31}; List<DayBean> dayBeans = new ArrayList<DayBean>(); List<DayBean> runDayBeans = new ArrayList<DayBean>(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_birthday); ButterKnife.bind(this); initWheelDate(); wheelDatePickerYear.setOnWheelChangeListener(new WheelPicker.OnWheelChangeListener() { @Override public void onWheelScrolled(int i) { } @Override public void onWheelSelected(int i) { updateYearValue(i+1900); } @Override public void onWheelScrollStateChanged(int i) { } }); wheelDatePickerMonth.setOnWheelChangeListener(new WheelPicker.OnWheelChangeListener() { @Override public void onWheelScrolled(int i) { } @Override public void onWheelSelected(int i) { int year = wheelDatePickerYear.getCurrentItemPosition()+1900; Log.e(TAG,"month i="+i); updateDayValue(year,i); } @Override public void onWheelScrollStateChanged(int i) { } }); } public void initWheelDate() { Calendar calendar = Calendar.getInstance(); Log.e(TAG,"calendar = "+calendar.toString()); //年 for(int i=1900;i<2018;i++){ yearList.add(i); } wheelDatePickerYear.setData(yearList); //月 for(int i=0;i<12;i++){ monthList.add(i+1); } wheelDatePickerMonth.setData(monthList); wheelDatePickerYear.setSelectedItemPosition(calendar.get(Calendar.YEAR)); wheelDatePickerMonth.setSelectedItemPosition(calendar.get(Calendar.MONTH)); //日 updateYearValue(wheelDatePickerYear.getCurrentItemPosition()+1900); wheelDatePickerDay.setSelectedItemPosition(calendar.get(Calendar.DAY_OF_MONTH)-1); } /* * 根据年份判断每月有几天 * */ public void updateYearValue(int year){ int month = wheelDatePickerMonth.getCurrentItemPosition(); if(isRunYear(year)){ for(int i=0;i<12;i++){ DayBean dayBean = new DayBean(); dayBean.setMonth(i+1); List<Integer> rundayList = new ArrayList<Integer>(); for(int j=1;j<=runDayArr[i];j++){ rundayList.add(j); dayBean.setDay(rundayList); } runDayBeans.add(dayBean); // Log.e(TAG,"rundaybeans="+runDayBeans.get(i).getMonth()+",days="+runDayBeans.get(i).getDay().toArray()); if(month ==i){ wheelDatePickerDay.setData(runDayBeans.get(month).getDay()); } } }else{ for(int i=0;i<12;i++){ DayBean dayBean = new DayBean(); dayBean.setMonth(i+1); List<Integer> dayList = new ArrayList<Integer>(); for(int j=1;j<=dayArr[i];j++){ dayList.add(j); dayBean.setDay(dayList); } dayBeans.add(dayBean); // Log.e(TAG,"daybeans="+dayBeans.get(i).getMonth()+",day="+dayBeans.get(i).getDay()); if(month ==i){ wheelDatePickerDay.setData(dayBeans.get(month).getDay()); } } } } /* * 根据年份和月份判断该月有几天 * */ public void updateDayValue(int year,int month){ if(isRunYear(year)){ for(int i=0;i<runDayBeans.size();i++){ if(month == i){ wheelDatePickerDay.setData(runDayBeans.get(i).getDay()); } } }else{ for(int i=0;i<dayBeans.size();i++){ if(month == i){ wheelDatePickerDay.setData(dayBeans.get(i).getDay()); } } } } public boolean isRunYear(int year){ if(year%4==0 && year%100 !=0 ||year%400 ==0){ System.out.println(year +"是闰年"); return true; } else{ System.out.println(year +"不是闰年!"); return false; } } }
布局文件:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto" xmlns:tools="http://schemas.android.com/tools" android:layout_marginLeft="20dp" android:layout_marginRight="20dp" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.example.castedemo.user.BirthdayActivity"> <TextView android:text="填写生日" android:gravity="center" android:layout_width="match_parent" android:layout_height="48dp" /> <View android:layout_above="@+id/ll_birth" android:layout_marginBottom="20dp" android:background="@color/text_gray" android:layout_width="match_parent" android:layout_height="0.3dp"/> <LinearLayout android:id="@+id/ll_birth" android:layout_centerInParent="true" android:layout_width="wrap_content" android:layout_height="wrap_content"> <com.itheima.wheelpicker.WheelPicker android:id="@+id/wheel_date_picker_year" android:layout_width="wrap_content" android:layout_height="wrap_content" app:wheel_atmospheric="true" app:wheel_curved="true" app:wheel_cyclic="true" app:wheel_selected_item_position="5" app:wheel_item_text_color="@color/text_white" app:wheel_selected_item_text_color="@color/orange"/> <TextView android:text="年" android:layout_marginLeft="6dp" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.itheima.wheelpicker.WheelPicker android:id="@+id/wheel_date_picker_month" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="16dp" app:wheel_atmospheric="true" app:wheel_curved="true" app:wheel_cyclic="true" app:wheel_selected_item_position="5" app:wheel_item_text_color="@color/text_white" app:wheel_selected_item_text_color="@color/orange"/> <TextView android:text="月" android:layout_marginLeft="6dp" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <com.itheima.wheelpicker.WheelPicker android:id="@+id/wheel_date_picker_day" android:layout_width="wrap_content" android:layout_height="wrap_content" app:wheel_atmospheric="true" android:layout_marginLeft="16dp" app:wheel_curved="true" app:wheel_cyclic="true" app:wheel_selected_item_position="5" app:wheel_item_text_color="@color/text_white" app:wheel_selected_item_text_color="@color/orange"/> <TextView android:text="日" android:layout_marginLeft="6dp" android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" /> </LinearLayout> <View android:id="@+id/view_bottom" android:layout_below="@+id/ll_birth" android:layout_marginTop="20dp" android:background="@color/text_gray" android:layout_width="match_parent" android:layout_height="0.3dp"/> <LinearLayout android:layout_marginTop="30dp" android:layout_below="@+id/view_bottom" android:gravity="center_horizontal" android:layout_width="match_parent" android:layout_height="wrap_content"> <Button android:id="@+id/btn_cancel" android:textColor="@color/text_white" android:background="@drawable/border_trans_style" android:text="取消" android:padding="5dp" android:layout_width="60dp" android:layout_height="wrap_content" /> <Button android:id="@+id/btn_save" android:background="@drawable/border_trans_style" android:textColor="@color/text_white" android:text="保存" android:padding="5dp" android:layout_marginLeft="20dp" android:layout_width="60dp" android:layout_height="wrap_content" /> </LinearLayout> </RelativeLayout>
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持猪先飞。
下一篇: Android实现扫码功能
相关文章
- 这篇文章主要介绍了在java中获取List集合中最大的日期时间操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧...2020-08-15
- 这篇文章主要介绍了教你怎么用Java获取国家法定节假日,文中有非常详细的代码示例,对正在学习java的小伙伴们有非常好的帮助,需要的朋友可以参考下...2021-04-23
- 这篇文章主要介绍了.NET/C# 使用Stopwatch测量运行时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2020-06-25
mysql中获取一天、一周、一月时间数据的各种sql语句写法
创建表:复制代码 代码如下:create table if not exists t( id int, addTime datetime default '0000-00-00 00:00:00′)添加两条初始数据:insert t values(1, '2012-07-12 21:00:00′);insert t values(2, '2012-07...2014-05-31- 常用的日期时间正则表达式 下面收藏了大量的日期时间正则匹配函数,包括分钟,时间与秒都能达到。 正则表达式 (?n:^(?=d)((?<day>31(?!(.0?[2469]|11))|30(?!.0?2)|29(...2016-11-25
- 实例讲解之前,先来介绍几个核心函数: mktime 函数 mktime() 函数返回一个日期的 Unix 时间戳。 参数总是表示 GMT 日期,因此 is_dst 对结果没有影响。 参数可以从右到左依次空着,空着的参数会被设为相应的当前 GMT 值。...2015-11-08
- 想在网页中动态地显示当前系统的时间,找了好多,不过都是一些停在那里不动的。。。不过皇天不负有心人,终于让我找到了...2020-06-25
- 这篇文章主要介绍了postgresql 中的时间处理小技巧(推荐),本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2021-03-29
- 我打开android开发手册的时候:http://www.csdn123.com/html/android/reference/packages.html 发现打开速度很慢,我用按了一下F12打开调试面板,切换到网络的选项卡network...2016-05-19
- /*解决代码高亮太长不换行*/ .syntaxhighlighter{word-break:break-all;} uParse('#newstext', {rootPath: '/e/extend/ueditor/'}) 帝国CMS显示指定时间内更新的信息数...2016-11-01
- 这篇文章主要给大家介绍了关于C#使用TimeSpan时间计算的相关资料,以及通过一个实例代码给大家介绍了C#使用timespan和timer完成一个简单的倒计时器的方法,需要的朋友可以参考借鉴,下面随着小编来一起学习学习吧...2020-06-25
- 这篇文章主要介绍了C# 当前系统时间获取及时间格式详解的相关资料,这里提供代码实例,帮助大家学习参考,需要的朋友可以参考下...2020-06-25
- 这篇文章主要介绍了C#获取文件创建时间的方法,涉及C#文件操作的技巧及CreattionTime属性的使用方法,需要的朋友可以参考下...2020-06-25
PowerShell中使用Get-Date获取日期时间并格式化输出的例子
这篇文章主要介绍了PowerShell中使用Get-Date获取日期时间并格式化输出的例子,本文讲解了直接调用Get-Date、在Write-Host中使用Get-Date、格式化输出的方法,需要的朋友可以参考下...2020-06-30php 获取今日、昨日、上周、本月的起始时间戳和结束时间戳的方法
php获取今日开始时间戳和结束时间戳$beginToday=mktime(0,0,0,date('m'),date('d'),date('Y'));$endToday=mktime(0,0,0,date('m'),date('d')+1,date('Y'))-1;//php获取昨日起始时间戳和结束时间...2013-10-04- 这篇文章主要介绍了Mybatis和Mybatis-Plus时间范围查询方式,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教...2021-08-06
- 这篇文章主要介绍了Vue 中获取当前时间并实时刷新,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下...2020-05-13
- 这篇文章主要介绍了python 获取域名到期时间的方法步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧...2021-02-10
- 这篇文章主要介绍了PHP如何通过date() 函数格式化显示时间,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下...2020-11-13
- 这篇文章主要介绍了在页面中输出当前客户端时间javascript实例代码的相关资料,需要的朋友可以参考下...2016-03-03