lmw
2023-06-16 03972ad1d3ce6ffe0be0395c0a4d5dcb4474031f
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
package cn.sinata.xldutils.widget
 
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import android.widget.EditText
import android.widget.NumberPicker
 
class MNumberPicker : NumberPicker, NumberPicker.Formatter {
    override fun format(value: Int): String {
        return if (displayedValues != null) {
            displayedValues[value]
        } else {
            ""
        }
 
    }
 
    constructor(context: Context) : super(context) {
        setFormatter(this)
    }
 
    constructor(context: Context, attrs: AttributeSet) : super(context, attrs) {
        setFormatter(this)
    }
 
    constructor(context: Context, attrs: AttributeSet, defStyleAttr: Int) : super(context, attrs, defStyleAttr) {
        setFormatter(this)
    }
 
    override fun addView(child: View) {
        super.addView(child)
        updateView(child)
    }
 
    override fun addView(child: View, index: Int,
                         params: android.view.ViewGroup.LayoutParams) {
        super.addView(child, index, params)
        updateView(child)
    }
 
    override fun addView(child: View, params: android.view.ViewGroup.LayoutParams) {
        super.addView(child, params)
        updateView(child)
    }
 
    fun updateView(view: View) {
        (view as? EditText)?.setTextColor(Color.parseColor("#333333"))
        (view as? EditText)?.setTextSize(TypedValue.COMPLEX_UNIT_SP,16f)
        (view as? EditText)?.isEnabled = false
        (view as? EditText)?.isFocusable = false
        (view as? EditText)?.isFocusableInTouchMode = false
        //分割线透明
        try {
            val pf = NumberPicker::class.java.getDeclaredField("mSelectionDivider")
            pf.isAccessible = true
            pf.set(this, ColorDrawable(Color.TRANSPARENT))
        } catch (e: NoSuchFieldException) {
            e.printStackTrace()
        } catch (e: IllegalAccessException) {
            e.printStackTrace()
        }
    }
 
}