lmw
2024-07-16 935a87b3578806ca37fee37f03da8c419a3896ce
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
152
153
154
155
156
157
158
159
160
161
package cn.sinata.xldutils.widget
 
import android.content.Context
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import android.os.Build
import android.text.TextUtils
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.DatePicker
import android.widget.EditText
import android.widget.LinearLayout
import android.widget.NumberPicker
import cn.sinata.xldutils.R
import org.jetbrains.anko.dip
import org.jetbrains.anko.padding
import org.jetbrains.anko.sp
 
import java.lang.reflect.Field
import java.util.ArrayList
import java.util.Calendar
 
/**
 *
 */
class MDatePicker : DatePicker {
    private var mPickers: MutableList<NumberPicker>? = null
 
    /**
     * 获得时间
     *
     * @return yyyy-mm-dd
     */
    /**
     * 设置时间
     *
     * @param strDate yyyy-mm-dd
     */
    var date: String
        get() {
            val sbDate = StringBuilder()
            sbDate.append(format2Digits(year)).append("-")
                    .append(format2Digits(month + 1)).append("-")
                    .append(format2Digits(dayOfMonth))
            return sbDate.toString()
        }
        set(strDate) {
            val day: Int
            val month: Int
            val year: Int
            if (!TextUtils.isEmpty(strDate)) {
                val dateValues = strDate.split("-".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
                if (dateValues.size == 3) {
                    year = Integer.parseInt(dateValues[0])
                    month = Integer.parseInt(dateValues[1]) - 1
                    day = Integer.parseInt(dateValues[2])
                    updateDate(year, month, day)
                    return
                }
            }
            val calendar = Calendar.getInstance()
            calendar.timeInMillis = System.currentTimeMillis()
            day = calendar.get(Calendar.DAY_OF_MONTH)
            month = calendar.get(Calendar.MONTH)
            year = calendar.get(Calendar.YEAR)
            updateDate(year, month, day)
        }
 
    constructor(context: Context) : super(context) {
        findNumberPicker()
    }
 
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        findNumberPicker()
    }
 
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        findNumberPicker()
    }
 
    /**
     * 得到控件里面的numberpicker组件
     */
    private fun findNumberPicker() {
        mPickers = ArrayList()
        val llFirst = getChildAt(0) as LinearLayout
        val mSpinners = llFirst.getChildAt(0) as LinearLayout
 
        for (i in 0 until mSpinners.childCount) {
            val picker = mSpinners.getChildAt(i) as NumberPicker
            mPickers!!.add(i, picker)
        }
    }
 
    private fun format2Digits(value: Int): String {
        return String.format("%02d", value)
    }
 
 
    /**
     * 设置picker间隔
     *
     * @param margin
     */
    fun setPickerMargin(margin: Int) {
        for (picker in mPickers!!) {
            val lps = picker.layoutParams as LinearLayout.LayoutParams
            lps.setMargins(margin, 0, margin, 0)
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) {
                lps.marginStart = margin
                lps.marginEnd = margin
            }
            picker.layoutParams = lps
        }
    }
 
    /**
     * 设置时间选择器的分割线颜色
     */
    fun setDividerColor(color: Int) {
        mPickers!!.indices
                .map { mPickers!![it] }
                .forEach {
                    try {
                        val pf = NumberPicker::class.java.getDeclaredField("mSelectionDivider")
                        pf.isAccessible = true
                        pf.set(it, ColorDrawable(color))
                    } catch (e: NoSuchFieldException) {
                        e.printStackTrace()
                    } catch (e: IllegalAccessException) {
                        e.printStackTrace()
                    }
                }
    }
 
    fun setTextSize(size: Float) {
        mPickers!!.indices
                .map { mPickers!![it] }
                .forEach {
                    try {
                        //这里改变初始化时的中间一行文字大小。
                        val editText = it.findViewById<View>(resources.getIdentifier("numberpicker_input","id","android"))
                        if (editText != null && editText is EditText) {
                            editText.setTextSize(TypedValue.COMPLEX_UNIT_SP,size)
                        }
                        //这里改变滚动后文字大小。
                        val pf = NumberPicker::class.java.getDeclaredField("mSelectorWheelPaint")
                        pf.isAccessible = true
                        if (pf.get(it) is Paint) {
                            (pf.get(it) as Paint).textSize = sp(size).toFloat()
                        }
                    } catch (e: NoSuchFieldException) {
                        e.printStackTrace()
                    } catch (e: IllegalAccessException) {
                        e.printStackTrace()
                    }
                }
    }
}