更改编号选择器选择的值值上的文本颜色已更改

时间:2018-09-25 15:42:14

标签: android kotlin android-number-picker

每当值更改时,我都试图更改NumberPicker当前选定的文本颜色。

这时我有以下代码:

val values2 = arrayOf(
    "00", "05", "10", "15", "20", "25", "30", "35", "40", "45", "50"
)

numberPicker2.maxValue = 10
numberPicker2.minValue = 0
numberPicker2.displayedValues = values2
numberPicker2.descendantFocusability = NumberPicker.FOCUS_BLOCK_DESCENDANTS

val myValChangedListener2 =
    NumberPicker.OnValueChangeListener { picker, oldVal, newVal ->
        val count = picker.childCount
        for (i in 0 until count) {
            val child = picker.getChildAt(i)
            if (child is EditText) {
                if (child.text.toString() == values2[newVal]) {
                    child.textColor = getColor(R.color.iberdrola_orange)
                    child.typeface =  Typeface.create("sans-serif-thin", Typeface.BOLD)
                } else {
                    child.typeface = Typeface.create("sans-serif-thin", Typeface.NORMAL)
                    child.textColor = getColor(R.color.black)
                }
            }
        }
        picker.invalidate()
    }
    numberPicker2.setOnScrollListener { view, scrollState ->
        if (view is EditText) {
            val count = numberPicker2.childCount
            for (i in 0 until count) {
                val child = numberPicker2.getChildAt(i) as EditText
                if (child == view) {
                    view.textColor = getColor(R.color.iberdrola_orange)
                } else {
                    child.textColor = getColor(R.color.black)
                }
            }
        }
    }
    numberPicker2.setOnValueChangedListener(myValChangedListener2)

NumberPicker所选值的颜色不会更改,至少在滚动并自动对齐到中心时不会更改。当我触摸选定的值时,它会变成橙色和粗体。

0 个答案:

没有答案