【发布时间】: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