无法设置按钮更改的背景颜色 state_enabled
Can't set backgound color of button changing state_enabled
我有一个用于输入 phone 号码和掩码的字段。我希望按钮颜色是,例如,当蒙版尚未完全填充时为黑色,填充时为黄色。
我的按钮
<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
删除这一行:
<item name="android:drawable">@color/selector_colored_button</item>
并使用:
<item name="android:backgroundTint">@color/selector_colored_button</item>
你可以看看我的
我有一个用于输入 phone 号码和掩码的字段。我希望按钮颜色是,例如,当蒙版尚未完全填充时为黑色,填充时为黄色。
我的按钮
<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
删除这一行:
<item name="android:drawable">@color/selector_colored_button</item>
并使用:
<item name="android:backgroundTint">@color/selector_colored_button</item>
你可以看看我的