如何以编程方式更改 RadioButton 背景的可绘制颜色?

How can I change the drawable color of RadioButton's background programmatically?

我的单选按钮具有可供选择器绘制的背景。

我想以编程方式更改 state_checked="true" 项的可绘制对象的颜色。

radiobutton的背景:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false"
        android:drawable="@drawable/checkbox_membertag_inactive" />
    <item android:state_checked="true"
        android:drawable="@drawable/checkbox_membertag_active"/>
</selector>

checkbox_membertag_active :

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <stroke
        android:width="1dp"
        android:color="@color/white" />
    <corners
        android:radius="36dp" />
    <solid
        android:color="@color/white" />
</shape>

我也尝试过这种方式。我想将活动项目的 @color/white 更改为我预先声明的 starColor。但它仍然是白色。

rgEventStyle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @RequiresApi(api = Build.VERSION_CODES.M)
            @SuppressLint("ResourceAsColor")
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton checkedEvtStyle = findViewById(checkedId);
                StateListDrawable stateListDrawable = (StateListDrawable) checkedEvtStyle.getBackground();
                stateListDrawable.addState(new int[]{android.R.attr.state_checked},new ColorDrawable(starColor));
                stateListDrawable.addState(new int[]{-android.R.attr.state_checked}, new ColorDrawable(R.color.gray5));
                checkedEvtStyle.setBackground(stateListDrawable);
            }
        });

尝试使用 ColorStatList,如下面的代码:

rgEventStyle.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
        @RequiresApi(api = Build.VERSION_CODES.M)
        @SuppressLint("ResourceAsColor")
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) {

            int[][] state =
                    {
                            new int[]{android.R.attr.state_checked},
                            new int[]{-android.R.attr.state_checked}
                    };
            int[] color = new int[]
                    {
                            ContextCompat.getColor(getApplicationContext(), R.starColor),
                            ContextCompat.getColor(getApplicationContext(), R.color.gray5)
                    };
            RadioButton checkedEvtStyle = findViewById(checkedId);
            ColorStateList colorStateList = new ColorStateList(state, color);
            checkedEvtStyle.setBackgroundTintList(colorStateList);
        }
    });