RadioButton使用

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