RadioButton和CheckBox的学习和使用

RadioButton

像在我们投递简历的时候会让我们选择性别:🚹或者🚺,这时候就会使用这个RadioButton,在一组数据当中选一个.

常用属性
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <!--
    android:checked="true" 表示默认选中
    android:orientation="vertical" 表示按钮水平放置
    android:orientation="horizontal" 表示按钮垂直放置
    -->
    <RadioGroup
        android:id="@+id/rg_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical">
        <RadioButton
            android:id="@+id/rb_1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:checked="true"
            android:textColor="#FF6600"
            android:textSize="18sp" />
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text=""
            android:id="@+id/rb_2"
            android:textColor="#FF6600"
            android:textSize="18sp"/>
    </RadioGroup>

    <RadioGroup
        android:id="@+id/rg_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal"
        android:layout_below="@id/rg_1"
        android:layout_marginTop="50dp">
        <RadioButton
            android:id="@+id/rb_3"
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:text=""
            android:gravity="center"
            android:button="@null"
            android:checked="true"
            android:textColor="#000000"
            android:textSize="18sp"
            android:background="@drawable/selector_orange_radiobutton"/>
        <RadioButton
            android:layout_width="60dp"
            android:layout_height="30dp"
            android:text=""
            android:gravity="center"
            android:button="@null"
            android:id="@+id/rb_4"
            android:textColor="#000000"
            android:textSize="18sp"
            android:layout_marginLeft="10dp"
            android:background="@drawable/selector_orange_radiobutton"/>
    </RadioGroup>

</RelativeLayout>
自定义样式
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <!--设置按钮在选中时的样子-->
    <item android:state_checked="true">
        <shape>
            <solid android:color="#AA6600" />
            <corners android:radius="5dp" />
        </shape>
    </item>
    <!--设置按钮在不选中的样子-->
    <item android:state_checked="false">
        <shape>
            <stroke android:width="1dp"
                android:color="#AA6600"/>
            <corners android:radius="5dp"/>
        </shape>
    </item>
</selector>
监听事件
public class RadioButtonActivity extends AppCompatActivity {
    /**
     * 声明一个RadioGroup
     */
    private RadioGroup mRg1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        mRg1 = findViewById(R.id.rg_1);
        mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                //获取到当前这个RadioButton之后
                RadioButton radioButton = group.findViewById(checkedId);
                //当点击这个RadioButton的时候打印它的相关信息
                Toast.makeText(RadioButtonActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}

复选框CheckBox

RadionButton是在一组选项当中选择一个,CheckBox是可以在一组选项当中选择多个选项

常用属性
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <TextView
        android:id="@+id/tv_title"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="10dp"
        android:text="你会从事那些移动开发:"
        android:textColor="#000000"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/tv_title"
        android:text="Android"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cb_1"
        android:text="IOS"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cb_2"
        android:text="H5"
        android:textSize="20sp" />

    <CheckBox
        android:id="@+id/cb_4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@id/cb_3"
        android:text="其他"
        android:textSize="20sp" />

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_below="@id/cb_4"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="你的兴趣"
            android:textColor="#000000"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_5"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginTop="10dp"
            android:button="@drawable/bg_checkbox"
            android:paddingLeft="10dp"
            android:text="编程"
            android:textSize="20sp" />

        <CheckBox
            android:id="@+id/cb_6"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:button="@drawable/bg_checkbox"
            android:paddingLeft="10dp"
            android:text="做饭"
            android:textSize="20sp" />
    </LinearLayout>
</RelativeLayout>
自定义样式

设置checkbox在选中和未选中时的样式

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item android:state_checked="false" android:drawable="@drawable/checkbox_false"/>
    <item android:state_checked="true" android:drawable="@drawable/checkbox_true"/>
</selector>
监听事件

设置checkbox在选中和未选中时触发的监听事件

public class RadioButtonActivity extends AppCompatActivity {
    /**
     * 声明一个RadioGroup
     */
    private RadioGroup mRg1;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_radio_button);
        mRg1 = findViewById(R.id.rg_1);
        mRg1.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton radioButton = group.findViewById(checkedId);
                Toast.makeText(RadioButtonActivity.this, radioButton.getText(), Toast.LENGTH_SHORT).show();
            }
        });
    }
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值