WheelPicker自定义时间选择器控件
作者:liuye066 时间:2023-05-16 18:30:05
本文实例为大家分享了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
标签:WheelPicker,时间,选择器
0
投稿
猜你喜欢
SpringBoot2.1.4中的错误处理机制
2023-11-06 02:48:47
C#实现json的序列化和反序列化实例代码
2022-07-18 01:50:34
Java详解Swing中的几种常用按钮的使用
2023-12-04 21:44:30
Java8通过Function获取字段名的步骤
2022-07-14 14:06:23
SpringBoot整合dataworks的实现过程
2023-11-29 12:13:09
C语言 socketpair用法案例讲解
2022-07-24 12:41:48
Hibernate环境搭建与配置方法(Hello world配置文件版)
2022-07-31 06:26:04
安卓(Android)动态创建多个按钮并添加监听事件
2023-04-25 16:11:43
Java读写文件,在文件中搜索内容,并输出含有该内容的所有行方式
2022-12-14 18:23:58
Android开发Popwindow仿微信右上角下拉菜单实例代码
2023-07-17 19:54:09
Android Studio 全屏沉浸式透明状态栏效果的实现
2021-07-16 06:42:41
Java如何构造DSL方法重构
2021-10-10 02:04:09
hibernate4基本配置方式详解
2023-03-11 11:07:43
Jenkins Pipeline 部署 SpringBoot 应用的教程详解
2022-09-26 14:06:27
Android Fragment使用全解
2021-07-11 22:57:09
C#使用Process类调用外部exe程序
2023-02-12 16:54:20
Java中的stream流的概念解析及实际运用总结
2022-06-10 23:19:10
Springboot框架实现自动装配详解
2023-03-03 21:11:14
c# AJAX实践VS2005 + RSSToolKit 开发你自己的RSS在线阅读器
2023-11-30 18:22:11
Java中的ThreadLocal详解
2023-01-02 16:02:21