【问题标题】:How to change checked color of Radio Button programmatically如何以编程方式更改单选按钮的选中颜色
【发布时间】:2019-05-20 00:08:18
【问题描述】:

我想知道它是否可能以编程方式以及在检查时如何以编程方式更改 RadioButton 的颜色?

PS : 我不想使用 XML

在 XML 中,我使用类似的东西及其工作:

    <RadioButton
        android:id="@+id/radio_button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="test1"
        android:textColor="@color/red"
        android:textSize="16dp"
        android:paddingStart="10dp"
        android:paddingEnd="0dp"
        android:theme="@style/CustomColorRadioButton" />

在我的 style.xml 中

<style name="CustomRadioButton" parent="AppTheme">
    <item name="colorControlActivated">@color/blue</item>
</style>

如何以编程方式做到这一点?

【问题讨论】:

标签: android kotlin


【解决方案1】:

试试这个:

ColorStateList colorStateList = new ColorStateList(
        new int[][]{
                new int[]{-android.R.attr.state_enabled}, //disabled
                new int[]{android.R.attr.state_enabled} //enabled
        },
        new int[] {
                Color.BLACK, //disabled
                Color.BLUE //enabled
        }
    );

radio.setButtonTintList(colorStateList);

见:Change Circle color of radio button- Android

【讨论】:

  • 检查你只是通过 state_checked 更改 state_enabled 并且它很好;)谢谢
  • 它也帮助了我。我认为它只适用于 20 以上的 SDK
猜你喜欢
  • 2020-05-22
  • 1970-01-01
  • 2016-11-06
  • 2010-12-21
  • 2016-03-24
  • 2018-11-20
  • 2011-05-14
  • 1970-01-01
相关资源
最近更新 更多