Android RadioButton设置选中时文字和背景颜色同时改变

在使用 RadioButton 时,有时我们会想要达到选中时文字颜色和背景颜色同时改变的效果,这里还需要多进行几步操作。

首先,在布局文件中新建一组 RadioButton :

            <RadioGroup
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:gravity="center"
                android:orientation="horizontal">

                <RadioButton
                    android:id="@+id/btn1"
                    android:layout_width="0dp"
                    android:layout_height="35dp"
                    android:layout_weight="1"
                    android:background="@drawable/radiobutton_background"
                    android:button="@null"
                    android:gravity="center"
                    android:text="P0501"
                    android:textColor="@color/radiobutton_textcolor"
                    android:textSize="14sp" />

                <RadioButton
                    android:id="@+id/btn2"
                    android:layout_width="0dp"
                    android:layout_height="35dp"
                    android:layout_marginStart="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/radiobutton_background"
                    android:button="@null"
                    android:gravity="center"
                    android:text="P0502"
                    android:textColor="@color/radiobutton_textcolor"
                    android:textSize="14sp" />

                <RadioButton
                    android:id="@+id/btn3"
                    android:layout_width="0dp"
                    android:layout_height="35dp"
                    android:layout_marginStart="10dp"
                    android:layout_weight="1"
                    android:background="@drawable/radiobutton_background"
                    android:button="@null"
                    android:gravity="center"
                    android:text="P0503"
                    android:textColor="@color/radiobutton_textcolor"
                    android:textSize="14sp" />

            </RadioGroup>
这里面有三个属性要做一下说明:

1、android:button="@null" 这样设置可以不显示我们通常所见的 RadioButton 中的圆形选中按钮.

2、android:background="@drawable/radiobutton_background" 这里设置了背景选择器,代码如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:drawable="@drawable/radiobutton_background_unchecked"
        android:state_checked="false" />
    <item android:drawable="@drawable/radiobutton_background_checked"
        android:state_checked="true" />
</selector>
这里面的选中样式又指向一个 Drawable 资源文件 radiobutton_background_checked.xml ,具体代码如下:

<shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle">
    <!-- 填充 -->
    <solid android:color="@color/color14" />
    <!-- 圆角 -->
    <corners android:radius="5dp" />
</shape>

以上这些资源文件都放在 res/drawable/ 目录下。

3、android:textColor="@color/radiobutton_textcolor" 这里设置了字体颜色选择器,需要稍作说明的是:需要在 res 目录下新建一个

文件夹取名为 color ,将字体颜色选择器 radiobutton_textcolor.xml 文件存放在 res/color/ 目录下面。代码如下:

<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:color="@color/color2"
        android:state_checked="false" />
    <item android:color="@color/color1"
        android:state_checked="true" />
</selector>
经过以上步骤后,我们来看一下效果图:

     

最后提一下怎么通过 RadioGroup 获取 RadioButton :

        RadioGroup radioGroup = (RadioGroup) findViewById(R.id.radioGroup);
        radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = (RadioButton) group.findViewById(checkedId);
                String result = radioButton.getText().toString();
            }
        });
这样就可以获取到当前 RadioGroup 中选中的 RadioButton ,然后进行一些你想要的操作。

  • 69
    点赞
  • 85
    收藏
    觉得还不错? 一键收藏
  • 24
    评论
要实现在选中 `RadioButton` 改变字体大小或字体颜色,可以使用以下方法: 1. 在布局文件中设置 `RadioButton` 的属性 `android:textColor` 或 `android:textSize`,并设置默认值。 2. 在 Java 代码中获取 `RadioButton` 对象,然后设置 `OnCheckedChangeListener` 监听器。 3. 在监听器的回调方法中,根据选中状态修改字体大小或字体颜色。 下面是一个示例代码: ```xml <RadioGroup android:id="@+id/radio_group" android:layout_width="match_parent" android:layout_height="wrap_content"> <!-- 设置默认字体颜色和大小 --> <RadioButton android:id="@+id/radio_button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 1" android:textColor="@color/default_text_color" android:textSize="16sp" /> <RadioButton android:id="@+id/radio_button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Option 2" android:textColor="@color/default_text_color" android:textSize="16sp" /> </RadioGroup> ``` ```java public class MainActivity extends AppCompatActivity { private RadioButton radioButton1, radioButton2; private RadioGroup radioGroup; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); radioGroup = findViewById(R.id.radio_group); radioButton1 = findViewById(R.id.radio_button1); radioButton2 = findViewById(R.id.radio_button2); // 设置监听器 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { // 判断选中RadioButton if (checkedId == R.id.radio_button1) { // 修改字体颜色和大小 radioButton1.setTextColor(Color.RED); radioButton1.setTextSize(20); // 恢复另一个 RadioButton 的默认样式 radioButton2.setTextColor(getResources().getColor(R.color.default_text_color)); radioButton2.setTextSize(16); } else if (checkedId == R.id.radio_button2) { radioButton2.setTextColor(Color.BLUE); radioButton2.setTextSize(20); radioButton1.setTextColor(getResources().getColor(R.color.default_text_color)); radioButton1.setTextSize(16); } } }); } } ``` 以上代码中,我们通过 `setOnCheckedChangeListener` 方法为 `RadioGroup` 添加了一个监听器,在选中状态改变修改字体颜色和大小。注意,在修改字体颜色需要使用 `Color` 类或 `getResources().getColor()` 方法获取颜色值。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 24
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值