android 自定义 radiobutton 文字颜色随选中状态而改变

先看效果



主要是写一个 color selector

在res/建一个文件夹取名color


res/color/color_radiobutton.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">


    <item android:state_checked="true" android:color="@color/color_text_selected"/>
    <!-- not selected -->
    <item android:color="@color/color_text_normal"/>


</selector>



程序中就可以直接使用了


//layout/main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
    <RadioGroup
        android:id="@+id/radiogroup_personal_condition"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >
        <RadioButton
            android:id="@+id/radiobutton_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_radio"
            android:button="@null"
            android:checked="true"
             android:gravity="center"
             android:text="目录"
            android:textColor="@color/color_radiobutton"
            android:textSize="@dimen/font_size"
            android:textStyle="bold" />
        <RadioButton
            android:id="@+id/radiobutton_2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:background="@drawable/selector_radio"
            android:button="@null"
             android:gravity="center"
             android:text="书签"
            android:textColor="@color/color_radiobutton"
            android:textSize="@dimen/font_size"
            android:textStyle="bold" />
    </RadioGroup>
</LinearLayout>



  • 7
    点赞
  • 15
    收藏
    觉得还不错? 一键收藏
  • 13
    评论
要自定义一个单选框(RadioButton),可以继承它的父类CompoundButton,然后在自定义类中实现需要改变字体文字颜色和单选框边框颜色的逻辑。 下面是一个示例代码: ```java public class CustomRadioButton extends RadioButton { private int mCheckedTextColor; private int mUncheckedTextColor; private int mCheckedBorderColor; private int mUncheckedBorderColor; public CustomRadioButton(Context context) { super(context); init(); } public CustomRadioButton(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomRadioButton(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } private void init() { mCheckedTextColor = Color.RED; mUncheckedTextColor = Color.BLACK; mCheckedBorderColor = Color.GREEN; mUncheckedBorderColor = Color.GRAY; } @Override public void setChecked(boolean checked) { super.setChecked(checked); if (checked) { setTextColor(mCheckedTextColor); setBorderColor(mCheckedBorderColor); } else { setTextColor(mUncheckedTextColor); setBorderColor(mUncheckedBorderColor); } } private void setBorderColor(int color) { GradientDrawable drawable = (GradientDrawable) getBackground(); drawable.setStroke(2, color); } } ``` 在上面的代码中,我们重写了setChecked()方法来实现选中和未选中改变字体文字颜色和单选框边框颜色的逻辑。同时,我们还定义了四个变量来存储选中和未选中状态下的字体文字颜色和单选框边框颜色。 在init()方法中,我们初始化了这四个变量的默认值。 在setChecked()方法中,我们首先调用父类的setChecked()方法来实现选中状态的切换。然后,根据选中状态来设置字体文字颜色和单选框边框颜色。 最后,我们还实现了一个setBorderColor()方法来设置单选框边框颜色。这里我们使用了GradientDrawable类来绘制单选框的背景,并通过setStroke()方法来设置边框的颜色和宽度。 使用自定义的单选框时,只需要在XML布局文件中引用即可: ```xml <com.example.CustomRadioButton android:id="@+id/custom_radio_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Custom Radio Button" /> ``` 注意,这里的包名需要替换成你自己的包名。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值