RadioButton使用

本文详细介绍了在Android开发中如何自定义RadioButton的颜色和图标,以及实现点击事件的两种方法。通过创建自定义的XML文件,可以轻松改变RadioButton的状态颜色,并通过在styles中添加自定义样式来替换默认图标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RadioButton和CheckBox差不多,这里只写一个,因为我本身不是学andorid,所以就当给自己留一个备份,省的每次用到都需要代码敲一次,很麻烦

1.如果想选中时想改变颜色可以设置一个xml,我设置的upload_state_btn
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_checked="true" android:color="#0000ff"></item>
        <item android:color="#2a2a2a"></item>
</selector>

 使用

<RadioGroup android:layout_width="match_parent"
                android:layout_height="44dp"
                android:orientation="horizontal"
        android:id="@+id/group">
        <RadioButton android:layout_width="0dp"
                     android:layout_height="match_parent"
                     android:layout_weight="1"
                     android:text="男"
                     android:button="@null"
                      android:gravity="center"
                     android:checked="true"
                     android:enabled="false"
                     android:textColor="@drawable/upload_state_btn"
                     android:id="@+id/btn1"
            />
        <RadioButton android:layout_width="0dp"
                     android:layout_height="match_parent"
                     android:layout_weight="1"
                     android:text="女"
                     android:button="@null"
                     android:gravity="center"
                     android:textColor="@drawable/upload_state_btn"
                     android:id="@+id/btn2"/>
    </RadioGroup>

 点击事件有两种方法

方法一:

  RadioGroup group = (RadioGroup)findViewById(R.id.group);
        group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId){
                    case R.id.btn1:
                        break;
                    case R.id.btn2:
                        break;
                }
            }
        });

 方法二:

   RadioButton btn1;
        RadioButton btn2;
        btn1 = (RadioButton)findViewById(R.id.btn1);
        btn2 = (RadioButton)findViewById(R.id.btn2);

        btn1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startIntent = new Intent(DemoActivity.this, MyService.class);
                //startService启动形式
                startService(startIntent);

            }
        });
        btn2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent startIntent = new Intent(DemoActivity.this, MyService.class);
                //startService启动形式
                stopService(startIntent);
            }
        });

 

如果需要改变系统自带的图标,可以添加xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
            android:drawable="@mipmap/checkbox_role"
             android:state_checked="true"/>
    <item
             android:drawable="@mipmap/checkbox_dis_role"
             android:state_checked="false"/>
    <item
           android:drawable="@mipmap/checkbox_dis_role"/>
</selector>

 在styles中添加:

 <style name="CustomTheme" parent="@android:style/Widget.CompoundButton.RadioButton">
        <item name="android:button">@drawable/custom_radio</item>
    </style>

 使用的使用直接在xml RadioButton属性中添加一句:

 style="@style/CustomTheme"

 

转载于:https://www.cnblogs.com/hualuoshuijia/p/10167394.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值