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

Android 如何以编程方式更改单选按钮的选中颜色,android,kotlin,Android,Kotlin,我想知道它是否可能以编程方式进行,以及如何在选中RadioButton时以编程方式更改其颜色 PS:我不想使用XML 在XML中,我使用以下内容及其工作: <RadioButton android:id="@+id/radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="te

我想知道它是否可能以编程方式进行,以及如何在选中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" />

在my style.xml中

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

@颜色/蓝色
如何以编程方式完成此操作?

试试以下方法:

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

请参阅:

radioButton.setBackgroundColor(getResources().getDrawable(R.color.yourColor));这一个的可能重复使用XML,我不想使用itCheck,您只需更改state启用的state\u,由state\u checked和它的good;)谢谢你也帮了我。我认为它只适用于20以上的SDK