1. 程式人生 > >Android基於wheelView的自定義日期選擇器(可拓展樣式)

Android基於wheelView的自定義日期選擇器(可拓展樣式)

基於wheelView的自定義日期選擇器

專案要求效果圖:

這裡寫圖片描述

要求 “6月20 星期五” 這一項作為一個整體可以滑動,”7時”、”48分”分別作為一個滑動整體。

系統自帶的DatePicker、TimePicker大家都知道,只有這種效果:

這裡寫圖片描述

百度了很多,試了NumberPicker等都不行,本來打算自己寫。網友推薦了一個開源元件WheelView,下下來試了試,發現他已經定義的很完善了,在他的基礎上拓展很容易。

現將基於wheelView自定義日期選擇器記錄如下:

一.首先要了解WheelView為我們提供了什麼:

這裡寫圖片描述

除了我寫的”DateObject”與”StringWheelAdapter”,其餘都是WheelView提供的,

1. WheelView.java : 可滾動的元件,

主要方法: 
setAdapter(new StringWheelAdapter(dateList, 7)); //設定Adapter 
setVisibleItems(3); //設定顯示幾行資料 
setCyclic(true); //設定是否迴圈顯示資料 
addChangingListener(onDaysChangedListener) //設定滑動監聽器

2. WheelAdapter.java : 滑動元件的介面卡的介面,子類介面卡用於裝載資料

public interface WheelAdapter {
    /**
     * Gets items count
     * @return
the count of wheel items */
public int getItemsCount(); /** * Gets a wheel item by index. * * @param index the item index * @return the wheel item text or null */ public String getItem(int index); /** * Gets maximum item length. It is used to determine the wheel width. * If -1 is returned there will be used the default wheel width. * * @return
the maximum item length or -1 */
public int getMaximumLength(); }
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

3. OnWheelChangedListener.java : 滑動監聽器介面

public interface OnWheelChangedListener {
    /**
     * Callback method to be invoked when current item changed
     * @param wheel the wheel view whose state has changed
     * @param oldValue the old value of current item
     * @param newValue the new value of current item
     */
    void onChanged(WheelView wheel, int oldValue, int newValue);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

4.OnWheelScrollListener.java :滾動監聽器介面(暫時沒用到)

5.NumericWheelAdapter.java : 當滾動內容為純數字時呼叫的介面卡

6.DateObject.java : 日期實體類,用於儲存、獲取選擇的資料

package kankan.wheel.widget;

import java.util.Calendar;

public class DateObject extends Object{
    private int year ;
    private int month;
    private int day;
    private int week;
    private int hour;
    private int minute;
    private String listItem;

    /**
     * 日期物件的4個引數構造器,用於設定日期
     * @param year
     * @param month
     * @param day
     * @author sxzhang
     */
    public DateObject(int year2, int month2, int day2,int week2) {
        super();
        this.year = year2;
        int maxDayOfMonth = Calendar.getInstance().getActualMaximum(Calendar.DAY_OF_MONTH);
        if(day2 > maxDayOfMonth){
            this.month = month2 + 1;
            this.day = day2 % maxDayOfMonth;
        }else{
            this.month = month2;
            this.day = day2;
        }
        this.week = week2 % 7 == 0 ? 7 : week2 % 7;

        if(day == Calendar.getInstance().get(Calendar.DAY_OF_MONTH)){
            this.listItem = String.format("%02d", this.month) +"月" + String.format("%02d", this.day)  + 
                    "日      "+ "  今天  ";
        }else{
            this.listItem = String.format("%02d", this.month) +"月" + String.format("%02d", this.day)  + 
                    "日      "+ getDayOfWeekCN(week);
        }

    }

    /**
     * 日期物件的2個引數構造器,用於設定時間
     * @param hour2
     * @param minute2
     * @param isHourType true:傳入的是hour; false: 傳入的是minute
     * @author sxzhang
     */
    public DateObject(int hour2,int minute2,boolean isHourType) {
        super();
        if(isHourType == true && hour2 != -1){      //設定小時
            if(hour2 > 24){
                this.hour = hour2 % 24;
            }else
                this.hour = hour2;
            this.listItem =  this.hour + "時";
        }else if(isHourType == false && minute2 != -1){ //設定分鐘
            if(minute2 > 60)
                this.minute = minute2 % 60;
            else
                this.minute = minute2;
            this.listItem =  this.minute + "分";
        }
    }

    public int getHour() {
        return hour;
    }

    public void setHour(int hour) {
        this.hour = hour;
    }

    public int getMinute() {
        return minute;
    }

    public void setMinute(int minute) {
        this.minute = minute;
    }

    public int getWeek() {
        return week;
    }

    public void setWeek(int week) {
        this.week = week;
    }

    public int getYear() {
        return year;
    }
    public void setYear(int year) {
        this.year = year;
    }
    public int getMonth() {
        return month;
    }
    public void setMonth(int month) {
        this.month = month;
    }
    public int getDay() {
        return day;
    }
    public void setDay(int day) {
        this.day = day;
    }
    public String getListItem() {
        return listItem;
    }
    public void setListItem(String listItem) {
        this.listItem = listItem;
    }

    /**
     * 根據day_of_week得到漢字星期
     * @return
     */
    public static String getDayOfWeekCN(int day_of_week){
        String result = null;
        switch(day_of_week){
        case 1:
            result = "星期日";
            break;
        case 2:
            result = "星期一";
            break;
        case 3:
            result = "星期二";
            break;
        case 4:
            result = "星期三";
            break;
        case 5:
            result = "星期四";
            break;
        case 6:
            result = "星期五";
            break;
        case 7:
            result = "星期六";
            break;  
        default:
            break;
        }
        return result;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151

7.StringWheelAdapter.java :一會兒將定義的滾動內容為字串的介面卡,當內容為字串時我們就可以隨意拓展滑動部分的內容

package kankan.wheel.widget;

import java.util.ArrayList;

/**
 * The simple String Array wheel adapter
 * 
 */
public class StringWheelAdapter implements WheelAdapter {

    /** The default items length */
    public static final int DEFAULT_LENGTH = -1;

    // items
    private ArrayList<DateObject> list;

    // length
    private int length;

    /**
     * Constructor
     * @param items the items
     * @param length the max items length
     */
    public StringWheelAdapter(ArrayList<DateObject> list, int length) {
        this.list = list;
        this.length = length;
    }


    @Override
    public String getItem(int index) {
        if (index >= 0 && index < list.size()) {
            return list.get(index).getListItem();
        }
        return null;
    }

    @Override
    public int getItemsCount() {
        return list.size();
    }

    @Override
    public int getMaximumLength() {
        return length;
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 相關推薦

    Android基於wheelView定義日期選擇(拓展樣式)

    基於wheelView的自定義日期選擇器 專案要求效果圖: 要求 “6月20 星期五” 這一項作為一個整體可以滑動,”7時”、”48分”分別作為一個滑動整體。 系統自帶的DatePicker、TimePicker大家都知道,只有這種效果:

    Android 仿 iphone 定義滾動選擇

    背景:其實我們都知道,在我們做開發的過程中,會遇到Android 自身所帶控制元件不夠的情況,那麼這個時候,就需要我們自定義控制元件,所以,也就造成了,在開發的過程中,我們一定要掌握好自定義控制元件,不然,當你去一家公司,產品萌妹子過來找你,這個效果很不錯,問你能不能實

    ios開發 定義日期選擇 PTXDatePickerView

    可能有一部分同學在專案中需要用到一個時間選擇器,又不想自己去自定義,那麼在這裡分享一個自定義的時間選擇器。可能直接用在你的專案中不合適,但可以給你提供參考,並快速自定義出一個時間選擇器。 首先描述下該

    Android定義時間選擇或者WheelView

    public class PickView extends View{ public static final String TAG = "PickerView"; /** * text之間間距和minTextSize之比 */ publ

    Android 定義數字選擇,可以根據自己的需求更改

    實現效果如下: 還是以往的套路,先把那些專案所需要的給展示出來。 values下的資料夾,attrs.xml <?xml version="1.0" encoding="utf-8"?> <resources> <declare-styl

    Android ListView三級聯動,實現定義地址選擇

    說到地址選擇器,好多小夥伴第一印象就是——wheelView~這玩意確實挺好用的^(* ̄(oo) ̄)^! 然而悲劇的故事發生了,傲嬌的老闆不喜歡wheelView那種選中條不動的效果 ~(⊙o⊙)! 好吧,其實是老闆不知道從哪個忘記名字的App看到這種效果,

    Android定義狀態選擇屬性

    其實,android已經給我們提供了比較豐富的狀態選擇器屬性了,比如android:state_checked、android:state_pressed、android:state_selected、android:state_enabled等等,相信大家都不

    定義Jquery選擇

    如果當前jQuery內建的選擇器不夠用,開發人員也可以擴充套件jQuery,實現使用者自定義的選擇器。 如下面建立一個具有綠色背景元素的選擇器。 <script> $(function(){ // 通過擴充套件$.expr[":"]實現自定義選擇器 $.expr[":

    定義時間選擇(更改分割線和距離)

    一、首先了解DatePicker原始碼的佈局        年、月、日 是由3個numberPicker組成  通過發射獲取到NumberPicker 如果不想顯示天數 ,重新佈局       lps.wid

    html+js定義顏色選擇

    選擇 wid htm borde 效果圖 html () alt mage <!DOCTYPE html><html><head> <meta charset="utf-8"> <title>test&

    springmvc配置定義日期轉換

    第一步、建立轉換器類實現Converter介面,重寫轉換邏輯。 import org.springframework.core.convert.converter.Converter; /** *

    定義時間選擇UIPickerView

    使用方法:@property (nonatomic, assign) QLChangeTimeView *chooseTimeView;//時間選擇檢視_chooseTimeView = [[NSBundle mainBundle]loadNibNamed:@"QLChangeTim

    SpringMVC+FastJson 定義日期轉換

    對於有的時候要輸出日期格式為yyyy-MM-dd,而有的時候要輸出yyyy-MM-dd hh:mm:ss時怎麼辦? 第一種方案:純註解式, 對日期型別的欄位進行註解 @JSONField(format = "yyyy-MM-dd") private Date upda

    安卓定義日曆選擇

    哈哈,又要更新部落格了+_+! 這一次發一個日曆選擇器吧! 說實話,看到需求那一刻,我絕對是崩潰的,不過還好有github這個大佬罩著我,所以為了避免重複造輪子,我就去上面找了一下日曆選擇器,一搜一大把,剛開始挺高興的,結果後來越看臉越黑,沒一個符合我的需求

    SpringMVC4.1+FastJson 定義日期轉換

    對於有的時候要輸出日期格式為yyyy-MM-dd,而有的時候要輸出yyyy-MM-dd hh:mm:ss時怎麼辦? 第一種方案:純註解式, 對日期型別的欄位進行註解 @JSONField(format = "yyyy-MM-dd") private Date updateDate; @JSONField(

    基於HTML5定義媒體播放

    簡要說明 顯示播放時間、可操控進度條,播放視訊是通過拖拽媒體檔案到播放器中即可播放。 注:上一曲、下一曲未實現。 素材 效果圖 style.css檔案 .mediaplayer { height

    android surfaceView+mediaPlayer 定義視訊播放

    Android 視訊播放一般分為兩種(其實3種,還一種是調Android自帶播放器): 1.使用Android自帶View VideoView ,其中一些功能,已經都給實現好了(此view一般不滿足自己的需求,比如:介面啊,控制啊,以及可能還有手勢控制音量,手勢控制亮度等等

    iOS 定義時間選擇 DatePicker

    6種時間選擇方式: 年月日、年月日時、年月日時分、時分、日時分、月日時分 呼叫方式 [[DatePicker shareManager]showWithType:PickerTypeDay title:nil time:@"2018:10:31" backT

    微信小程式-定義單項選擇樣式

    效果: wxml: <view bindchange="radioChange"> <view class='list_item' wx:for="{{radioVa

    定義ckeditor編輯的工具樣式

    //字型. config.font_names = ‘宋體;楷體_GB2312;新宋體;黑體;隸書;幼圓;微軟雅黑;Arial; Comic Sans MS;Courier New;Tahoma;Times New Roman;Verdana;’ ; //工具按