【问题标题】:Can't set backgound color of button changing state_enabled无法设置按钮更改 state_enabled 的背景颜色
【发布时间】:2020-08-06 00:28:05
【问题描述】:

我有一个用于输入电话号码和掩码的字段。我希望按钮颜色为,例如,当蒙版尚未完全填充时为黑色,在填充时为黄色。

我的按钮

<Button
    android:id="@+id/buttonNext"
    style="@style/AppTheme.YellowButton"
    android:text="Далее"/>

风格

<style name="AppTheme.YellowButton" parent="Widget.MaterialComponents.Button.TextButton">
    <item name="android:textSize">@dimen/text_size_20sp</item>
    <item name="android:textColor">@color/color_accent</item>
    <item name="android:textAllCaps">false</item>
    <item name="android:textStyle">bold</item>
    <item name="android:backgroundTint">@color/yellow_mail</item>
    <item name="android:drawable">@color/selector_colored_button</item>
</style>

@color/selector_colored_button

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_enabled="true" android:color="@color/yellow_mail"/>
    <item android:state_enabled="false" android:color="@android:color/black"/>
</selector>

及其代码

MaskedTextChangedListener.installOn(
    editText = editPhone,
    primaryFormat = "[000] [000]-[00]-[00]",
    valueListener = object : MaskedTextChangedListener.ValueListener {
        override fun onTextChanged(
            maskFilled: Boolean,
            extractedValue: String,
            formattedValue: String
        ) {
            buttonNext.isEnabled = maskFilled
        }
    })

但是按钮一直是黄色的,它的背景没有改变。不知道我做错了什么。有什么帮助吗? 我正在使用"com.google.android.material:material:1.2.0-alpha06"Theme.MaterialComponents.DayNight.NoActionBar

【问题讨论】:

    标签: android material-design android-styles material-components-android material-components


    【解决方案1】:

    删除这一行:

    <item name="android:drawable">@color/selector_colored_button</item>
    

    并使用:

    <item name="android:backgroundTint">@color/selector_colored_button</item>
    

    你可以看看我的answer

    【讨论】:

      猜你喜欢
      • 2020-01-01
      • 2015-06-04
      • 1970-01-01
      • 2021-06-22
      • 2015-04-03
      • 1970-01-01
      相关资源
      最近更新 更多