获取多个复选框的值

当有多个复选框CheckBox时,选中其中的某几个复选框时候,判断选中的都是哪几个,实现方式如下:
xml布局文件

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <CheckBox
        android:id="@+id/cb1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="体育"/>

    <CheckBox
        android:id="@+id/cb2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="音乐"/>

    <CheckBox
        android:id="@+id/cb3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="美术"/>

    <CheckBox
        android:id="@+id/cb4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="英语"/>
    <CheckBox
        android:id="@+id/cb5"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="语文"/>

    <Button
        android:id="@+id/btn_submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="提交"/>

    <TextView
        android:id="@+id/tv_jsonString"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="jsonString:"/>

</LinearLayout>

类文件主要功能实现

private void initView() {
        cb1 = (CheckBox) findViewById(R.id.cb1);
        cb2 = (CheckBox) findViewById(R.id.cb2);
        cb3 = (CheckBox) findViewById(R.id.cb3);
        cb4 = (CheckBox) findViewById(R.id.cb4);
        cb5 = (CheckBox) findViewById(R.id.cb5);

        // 将所有的checkbox放到一个集合中
        checkBoxList.add(cb1);
        checkBoxList.add(cb2);
        checkBoxList.add(cb3);
        checkBoxList.add(cb4);
        checkBoxList.add(cb5);

        btn_submit = (Button) findViewById(R.id.btn_submit);
        btn_submit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                checkBoxInfoList = new ArrayList<CheckBoxInfo>();
                //遍历集合中的checkBox,判断是否选择,获取选中的文本添加到实体类CheckBoxInfo里
                //并存入列表checkBoxInfoList里
                for (CheckBox checkbox : checkBoxList) {
                    if (checkbox.isChecked()) {
                        CheckBoxInfo checkBoxInfo = new CheckBoxInfo();
                        checkBoxInfo.setLike(checkbox.getText().toString());
                        checkBoxInfoList.add(checkBoxInfo);
                    }
                }
            }
        });
    }

CheckBoxInfo实体类的源码如下:

import java.io.Serializable;

/**
 * Created by WangJinyong on 2017/9/20.
 */

public class CheckBoxInfo implements Serializable {

    private String like;

    public String getLike() {
        return like;
    }

    public void setLike(String like) {
        this.like = like;
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

时代新人

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值