WheelPicker自定义时间选择器控件
作者:liuye066 发布时间:2023-05-16 18:30:05
标签:WheelPicker,时间,选择器
本文实例为大家分享了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>
来源:https://blog.csdn.net/liuye066/article/details/78362691


猜你喜欢
- 中介者模式面向对象设计鼓励将行为分布到各个对象中, 这种分布可能会导致对象间有许多连接. 在最坏的情况下, 每一个对象都需要知道其他所有对象
- BeanFactoryBeanFactory是Spring中容器功能的基础,用于存放所有已经加载的bean,为了保证程序上的高可扩展性,Sp
- OAuth 简介OAuth 是由 Blaine Cook、Chris Messina、Larry Halff 及 David Recordo
- 消息的保存路径消息发送端发送消息到 broker 上以后,消息是如何持久化的?数据分片kafka 使用日志文件的方式来保存生产者和发送者的消
- 前言最近项目需要和Oracle数据库进行交互,然后我从Maven中央仓库下载数据库驱动jar包,但怎么都下不下来,我到Oracle官网上一看
- 页眉位于文档中每个页面的顶部区域,常用于显示文档的附加信息,可以插入时间、图形、公司微标、文档标题、文件名或作者姓名等;页脚位于文档中每个页
- Java中的main函数的详细介绍JAVA中的主函数是我们再熟悉不过的了,相信每个学习过JAVA语言的人都能够熟练地写出这个程序的入口函数,
- ManualResetEvent表示线程同步事件,可以对所有进行等待的线程进行统一管理(收到信号时必须手动重置该事件)其构造函数为:publ
- LinkedList简介LinkedList是一个使用双向链表结构实现的容器,与ArrayList一样,它能动态扩充其长度,LinkedLi
- [LeetCode] 9. Palindrome Number 验证回文数字Determine whether an integer is
- 之前在学习RecyclerView的时候,建立了一个可以滑动的View列表,但是当滑动距离过长的时候,需要手动返回到顶部,于是加了一个一键返
- 今天写一个小程序中使用到了全局快捷键,找到了我之前写的文章在c#中使用全局快捷键翻了一下,发现它是WinForm版本的,而我现在大部分写WP
- 本文介绍spring-rest接口中的LocalDateTime日期类型转时间戳的方法。具体的代码参照示例项目 https://github
- 最近一段时间在研究OAuth2的使用,想整个单点登录,从网上找了很多demo都没有实施成功,也许是因为压根就不懂OAuth2的原理导致。有那
- LayoutInflater.inflate源码详解LayoutInflater的inflate方法相信大家都不陌生,在Fragment的o
- 安装方法一:1.Android studio File->Settings..->Plugins–>Browse repo
- 简介简单工厂模式 (Simple Factory) 又叫静态工厂方法(Static Factory Method)模式。简单工厂模式通常是定
- 本文主要介绍了Java实现雪花算法(snowflake),分享给大家,具体如下:简单描述最高位是符号位,始终为0,不可用。41位的时间序列,
- 先看一下效果图:<?xml version="1.0" encoding="utf-8"?&g
- 1 请求映射 在SpringBoot中使用@XxxMapping注解完成前端请求与后端方法的一个映射。以前的时候,通常使用url映射命名的