WheelPicker自定义时间选择器控件

本文实例为大家分享了WheelPicker自定义时间选择器控件的具体代码,供大家参考,具体内容如下

先上图:

WheelPicker自定义时间选择器控件_第1张图片

使用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中:

  • 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 yearList = new ArrayList();
    List monthList = new ArrayList();
    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 dayBeans = new ArrayList();
    List runDayBeans = new ArrayList();

    @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 rundayList = new ArrayList();
                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 dayList = new ArrayList();
                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 
 

布局文件:



    
    
    
        
        
        
        
        
        
    
    

    
        

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

你可能感兴趣的:(WheelPicker自定义时间选择器控件)