我正在尝试更改NumberPicker的文本颜色!
我希望在不同的状态下使用不同的颜色,如果启用状态并禁用状态的不同颜色。
这是我的号码选择码:
<NumberPicker
android:id="@+id/NumberOfWebsitesPicker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleX="0.75"
style="@style/MyStyle"
android:descendantFocusability="blocksDescendants"
android:scaleY="0.75" />
这是
下的text_color.xmlRes-&gt; Color Direcoty
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#9D9FA2" />
<item android:state_enabled="true" android:color="#FAFAFA" />
</selector>
现在这是我的style.xml
<resources>
<!-- Base application theme. -->
<style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
<item name="colorPrimary">@color/colorPrimary</item>
<item name="colorPrimaryDark">@color/colorPrimaryDark</item>
<item name="colorAccent">@color/colorAccent</item>
<item name="android:colorBackground">@color/colorBackground</item>
<!-- It should be true otherwise action mode will not overlay toolbar -->
<item name="windowActionModeOverlay">true</item>
<!-- For Custom Action Mode Background Color/Drawable -->
<item name="actionModeBackground">@color/colorPrimary</item>
<item name="android:textColor">@color/text_color</item>
<item name="android:textColorPrimary">@color/text_color</item>
<item name="android:colorControlNormal">@color/divider_color</item>
<item name="android:divider">@color/divider_color</item>
<item name="android:textColorHint">@color/colorPrimary</item>
<item name="android:textSize">@dimen/font_size_14</item>
<item name="actionBarSize">56dp</item>
<item name="android:spinnerItemStyle">@style/Spinner_Item_Style</item>
<item name="android:spinnerDropDownItemStyle">@style/Spinner_DropDown_Item_Style</item>
<item name="android:dropDownListViewStyle">@style/SpinnerStyle</item>
</style>
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">2dp</item>
<item name="android:background">@color/divider_color</item>
</style>
<style name="DividerThin">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">2px</item>
<item name="android:background">@color/divider_color</item>
</style>
<style name="Vertical_divider" parent="DividerThin">
<item name="android:layout_width">2px</item>
<item name="android:background">@color/divider_color</item>
<item name="android:layout_height">match_parent</item>
</style>
<style name="Vertical_divider_thick" parent="Divider">
<item name="android:layout_width">2dp</item>
<item name="android:background">@color/divider_color</item>
<item name="android:layout_height">match_parent</item>
</style>
<style name="Spinner_Item_Style">
<item name="android:gravity">center</item>
<item name="android:textAlignment">center</item>
</style>
<style name="Spinner_DropDown_Item_Style" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:gravity">center</item>
<item name="android:textAlignment">center</item>
<item name="android:paddingStart">@dimen/unit_8dp</item>
<item name="android:paddingEnd">@dimen/unit_8dp</item>
</style>
<style name="SpinnerStyle" parent="Widget.AppCompat.ListView.DropDown">
<item name="android:divider">@color/divider_color</item>
<item name="android:dividerHeight">1px</item>
</style>
<style name="MyStyle">
<item name="android:colorPrimary">@color/text_color</item>
</style>
</resources>
现在这里它只能运行一次,下次当数字选择器被禁用时,它会保持其颜色为白色,并且不会改变颜色,并在text_color选择器文件中声明其禁用的颜色。
我不知道为什么它只工作一次,
如果在CreateCicker上禁用了Numberpicker,它将获得禁用状态的颜色 当我通过按下按钮手动禁用选择器时,它会将文本颜色更改为禁用颜色,但是当我启用它时,它会采用启用颜色的颜色并更改值并再次禁用它,它不会转到禁用颜色。
它只能运行一次,并且在更改选择器的值后不起作用。
答案 0 :(得分:0)
我通过使用以下代码解决了此问题:
fun setEnabledNumberPicker(numberPicker: NumberPicker, isEnable : Boolean) {
numberPicker.isEnabled = isEnable
val enablePickerColor = ContextCompat.getColor(requireContext(), R.color.textEnabled)
val disablePickerColor = ContextCompat.getColor(requireContext(), R.color.textDisabled)
try {
val selectorWheelPaintField: Field = numberPicker.javaClass
.getDeclaredField("mSelectorWheelPaint")
selectorWheelPaintField.isAccessible = true
if (isEnable)
(selectorWheelPaintField.get(numberPicker) as Paint).color = enablePickerColor
else
(selectorWheelPaintField.get(numberPicker) as Paint).color = disablePickerColor
} catch (e: NoSuchFieldException) {
e.printStackTrace()
} catch (e: IllegalAccessException) {
e.printStackTrace()
} catch (e: IllegalArgumentException) {
e.printStackTrace()
}
val count = numberPicker.childCount
for (i in 0 until count) {
val child = numberPicker.getChildAt(i)
if (child is EditText) {
if (isEnable)
child.setTextColor(enablePickerColor)
else
child.setTextColor(disablePickerColor)
}
}
numberPicker.invalidate()
}