【发布时间】:2019-05-15 16:15:45
【问题描述】:
我有两个按钮,我想在按下一个按钮时实现蓝色边框。当按钮按下时,它的边框不会变成蓝色,另一个按钮的边框恢复默认。
我无法正确返回默认边框。我写了函数,但是工作错了。
我的按钮(第二个与另一个id和文本相同):
<com.google.android.material.button.MaterialButton
android:id="@+id/btn_schemes_1"
style="@style/Buttons.Schemes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="@dimen/margin_4"
android:layout_weight="1"
android:text="@string/schemes_1"
app:icon="@drawable/ic_schemes_1" />
风格没有什么有趣的:
<style name="Buttons.Schemes" parent="Widget.MaterialComponents.Button.OutlinedButton">
<item name="android:typeface">sans</item>
<!--android:fontFamily="sans-serif-condensed-medium"-->
<item name="android:textColor">@android:color/black</item>
<item name="android:letterSpacing">0</item>
<item name="iconTint">@null</item>
<item name="backgroundTint">@android:color/white</item>
</style>
我最后的认识是在第一次点击之前找到并记住默认边框值,所以我首先写:
private var defaultColor: Int = 0
然后
defaultColor = btn_schemes_1.strokeColor.defaultColor
和函数,当点击其中一个按钮时调用:
private fun setButtonsBorder(buttonNumber: Int) {
when (buttonNumber) {
1 -> {
btn_schemes_1.strokeColor = ColorStateList.valueOf(Color.BLUE)
btn_schemes_1.strokeWidth = 2
btn_schemes_2.strokeColor = ColorStateList.valueOf(defaultColor)
btn_schemes_2.strokeWidth = 1
}
2 -> {
btn_schemes_1.strokeColor = ColorStateList.valueOf(defaultColor)
btn_schemes_1.strokeWidth = 1
btn_schemes_2.strokeColor = ColorStateList.valueOf(Color.BLUE)
btn_schemes_2.strokeWidth = 2
}
}
}
但这也失败了,因为默认颜色实际上不是“那些”默认值。当我设置它时,我发现它只是colorPrimary。
如何找到此默认颜色或编写方法将边框颜色恢复为默认值?
【问题讨论】:
标签: android kotlin android-button