【问题标题】:How can I change the drawable color of RadioButton's background programmatically?如何以编程方式更改 RadioButton 背景的可绘制颜色?
【发布时间】:2021-07-04 20:16:55
【问题描述】:

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

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

单选按钮的背景:

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

【问题讨论】:

    标签: java android radio-button background-color drawable


    【解决方案1】:

    尝试使用 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);
            }
        });
    

    【讨论】:

      猜你喜欢
      • 2012-07-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-12-21
      • 1970-01-01
      • 2023-03-02
      • 1970-01-01
      相关资源
      最近更新 更多