WheelPicker自定義時(shí)間選擇器控件
本文實(shí)例為大家分享了WheelPicker自定義時(shí)間選擇器控件的具體代碼,供大家參考,具體內(nèi)容如下
先上圖:

使用android自帶的DatePicker控件雖然也能實(shí)現(xiàn)功能,但樣式不能改變。想要實(shí)現(xiàn)一些 自定義的樣式,就要用到WheelPicker了。
要使用WheelPicker,需要先導(dǎo)入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.復(fù)制如下代碼到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 : 條目顏色是否執(zhí)行銜接處理 效果更好
- wheel_curved : 是否是弧形狀態(tài)顯示
- wheel_cyclic : 是否可循環(huán)
- wheel_selected_item_position : 默認(rèn)選中第幾個(gè)條目
然后使用即可。
頁(yè)面代碼:
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);
}
/*
* 根據(jù)年份判斷每月有幾天
* */
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());
}
}
}
}
/*
* 根據(jù)年份和月份判斷該月有幾天
* */
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>
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android SdkVersion的區(qū)別及獲取版本信息方法
下面小編就為大家?guī)硪黄狝ndroid SdkVersion的區(qū)別及獲取版本信息方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03
Android常用控件ImageSwitcher使用方法詳解
這篇文章主要為大家詳細(xì)介紹了Android常用控件ImageSwitcher的使用方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-08-08
android ListActivity顯示圖標(biāo)實(shí)例
在ListActivity中顯示圖標(biāo),好像并不復(fù)雜,實(shí)現(xiàn)起來卻不輕松,我們下面一步步來實(shí)現(xiàn)ListActivity中顯示圖標(biāo)2013-11-11
Android應(yīng)用退出登錄的實(shí)現(xiàn)方法
每一個(gè)app都會(huì)有一個(gè)”退出登陸”的功能,當(dāng)點(diǎn)擊退出之后需要將所有的Activity都finish掉,開始是想將棧中的所有Activity清除掉,但是沒有找到方法,后來用廣播實(shí)現(xiàn)了。下面小編給大家分享android應(yīng)用退出登錄的實(shí)現(xiàn)方法,需要的朋友參考下2017-04-04
Android自定義控件eBook實(shí)現(xiàn)翻書效果實(shí)例詳解
這篇文章主要介紹了Android自定義控件eBook實(shí)現(xiàn)翻書效果的方法,結(jié)合實(shí)例形式分析了Android自定義控件實(shí)現(xiàn)翻書效果的具體步驟與相關(guān)操作技巧,需要的朋友可以參考下2016-10-10
ViewPager實(shí)現(xiàn)輪播圖引導(dǎo)頁(yè)
這篇文章主要為大家詳細(xì)介紹了ViewPager實(shí)現(xiàn)輪播圖引導(dǎo)頁(yè),文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2022-09-09
Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)調(diào)用攝像頭拍照與視頻功能,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-04-04

