如何以编程方式更改单选按钮的选中颜色

问题描述:

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

I would like to know if its possible programmatically and how to change programmatically the color of RadioButton when it checked ?

PS:我不想使用XML

PS : I dont want to use XML

在XML中,我使用类似的方法及其工作:

in XML I use something like this and its work :

    <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

In my style.xml

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

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

How can I do that programmatically ?

尝试一下:

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);

请参阅:更改单选按钮的圆形颜色-Android