如何以编程方式更改单选按钮的选中颜色 [英] How to change checked color of Radio Button programmatically

查看:49
本文介绍了如何以编程方式更改单选按钮的选中颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道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

这篇关于如何以编程方式更改单选按钮的选中颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆