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
0
投稿
猜你喜欢
- 效果:原图加水印后的图片废话不多说,直接上代码代码:package com.example.demo;import java.awt.Alp
- 最近经常有人问Spring Cloud Feign如何上传文件。有团队的新成员,也有其他公司的兄弟。本文简单做个总结——早期的Spring
- 流,就是一系列的数据。当不同介质之间有数据交互的时候,JAVA就使用流来实现。数据源可以是文件,还可以是数据库、网络甚至其他的程序。比如读取
- 本文实例为大家分享了java转树形结构工具类的具体代码,供大家参考,具体内容如下import com.alibaba.fastjson.JS
- 使用JDBC时,我们都会很自然得使用下列语句:Class.forName("com.mysql.jdbc.Driver"
- 调试的时候,在循环里增加条件判断,可以极大的提高效率,心情也能愉悦。以下介绍下IDEA使用条件【Condition】断点的方法1、编写一段样
- intellij idea是一款非常优秀的软件开发工具,它拥有这强大的插件体系,可以帮助开发者完成很多重量级的功能。今天,我们来学习一下如何
- Jenkins是一个开源软件项目,旨在提供一个开放易用的软件平台,使软件的持续集成变成可能Jenkins是基于Java开发的一种持续集成工具
- 在Java 8之前,对集合进行排序需要为排序中使用的比较器 Comparator 创建一个匿名内部类:new Compa
- 在进行持久层数据维护(新增或修改)的时候,我们通常需要记录一些非业务字段,比如:create_time、update_time、update
- 这篇文章主要介绍了Java import导入及访问控制权限修饰符过程解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 1、public String(char[] c,begin,length).从字符数组c的下标begin处开始,将长度为length的字符
- 小程序官方流程图如下,官方地址 : https://developers.weixin.qq.com/miniprogram/dev/fra
- import java.io.BufferedInputStream;import java.io.BufferedOutputStream
- 看门见山1.java中replace API:replace(char oldChar, char newChar):寓意为:返回一个新的字
- mybatis in foreach 双层嵌套需求:根据角色id,返回角色名称,入参是List<List> 类型的二维数组,需要
- 本文实例为大家分享了Java实现简易计算器的具体代码,供大家参考,具体内容如下程序的运行环境为Windows10 ,编译环境为IDEA。计算
- 前言MyBatis-Plus 是一个 MyBatis 的增强工具,在 MyBatis 的基础上只做增强不做改变,为简化开发、提高效率而生。那
- WPF 实现步骤控件框架使用.NET40;Visual Studio 2019;Step 继承 ItemsControl
- 一、前期准备1、申请好微信商户号appid,拿到商户id和商户秘钥,退款的话需要商户证书2、申请好支付宝商户号appid,商户公钥和秘钥(需