本文实例为大家分享了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 && year0 !=0 ||year@0 ==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>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持Devmax。

WheelPicker自定义时间选择器控件的更多相关文章

  1. 微信小程序多项选择器checkbox

    这篇文章主要为大家详细介绍了微信小程序多项选择器checkbox,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  2. 微信小程序实现根据日期和时间排序功能

    这篇文章主要为大家详细介绍了微信小程序实现根据日期和时间排序功能,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  3. jQuery日期范围选择器附源码下载

    jQuery Date Range Picker是一款允许用户选择一个日期时间范围的jQuery日期选择器插件。下面通过本文给大家分享jQuery日期范围选择器的实现思路,需要的的朋友参考下吧

  4. 关于导入excel时js转换时间的正确方式

    这篇文章主要给大家介绍了关于导入excel时js转换时间的正确方式,以及js读取excel中日期格式转换问题的相关资料,文中通过示例代码介绍的非常详细,需要的朋友可以参考下

  5. Python执行时间计算方法以及优化总结

    python脚本运行时间远远大于python脚本中统计的计算时间,所以本文将为大家分享就几个Python执行时间计算方法以及优化,感兴趣的可以了解一下

  6. iOS获取当前时间和当前时间戳的方法

    这篇文章主要介绍了iOS获取当前时间和当前时间戳,获取当前时间戳有两种方法以秒位单位的,下面通过本文给大家分享iOS获取当前时间和当前时间戳的方法,一起看看吧

  7. iOS如何获取当前日期前后N天的时间示例代码

    这篇文章主要给大家介绍了关于iOS如何获取当前日期前后N天的时间的相关资料,文中通过示例代码介绍的非常详细,对各位iOS开发者们具有一定的参考学习价值,需要的朋友们下面随着小编来一起看看吧。

  8. jQuery选择器之子元素过滤选择器

    这篇文章主要为大家详细介绍了jQuery选择器之子元素过滤选择器的相关资料,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. jQuery选择器及jquery案例详解(必看)

    本文给大家介绍jquery选择器的相关知识,并通过案例给大家介绍jquery知识,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起学习吧

  10. java8 时间日期的使用与格式化示例代码详解

    这篇文章主要介绍了java8 时间日期的使用与格式化,本文通过示例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

随机推荐

  1. Flutter 网络请求框架封装详解

    这篇文章主要介绍了Flutter 网络请求框架封装详解,小编觉得挺不错的,现在分享给大家,也给大家做个参考。一起跟随小编过来看看吧

  2. Android单选按钮RadioButton的使用详解

    今天小编就为大家分享一篇关于Android单选按钮RadioButton的使用详解,小编觉得内容挺不错的,现在分享给大家,具有很好的参考价值,需要的朋友一起跟随小编来看看吧

  3. 解决android studio 打包发现generate signed apk 消失不见问题

    这篇文章主要介绍了解决android studio 打包发现generate signed apk 消失不见问题,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧

  4. Android 实现自定义圆形listview功能的实例代码

    这篇文章主要介绍了Android 实现自定义圆形listview功能的实例代码,本文通过实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  5. 详解Android studio 动态fragment的用法

    这篇文章主要介绍了Android studio 动态fragment的用法,本文给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友可以参考下

  6. Android用RecyclerView实现图标拖拽排序以及增删管理

    这篇文章主要介绍了Android用RecyclerView实现图标拖拽排序以及增删管理的方法,帮助大家更好的理解和学习使用Android,感兴趣的朋友可以了解下

  7. Android notifyDataSetChanged() 动态更新ListView案例详解

    这篇文章主要介绍了Android notifyDataSetChanged() 动态更新ListView案例详解,本篇文章通过简要的案例,讲解了该项技术的了解与使用,以下就是详细内容,需要的朋友可以参考下

  8. Android自定义View实现弹幕效果

    这篇文章主要为大家详细介绍了Android自定义View实现弹幕效果,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  9. Android自定义View实现跟随手指移动

    这篇文章主要为大家详细介绍了Android自定义View实现跟随手指移动,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

  10. Android实现多点触摸操作

    这篇文章主要介绍了Android实现多点触摸操作,实现图片的放大、缩小和旋转等处理,文中示例代码介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们可以参考一下

返回
顶部