获取多个复选框的值
来源:互联网 发布:怎么看自己的淘宝联盟i 编辑:程序博客网 时间:2024/04/20 08:58
当有多个复选框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; }}
阅读全文
0 0
- 获取多个复选框的值
- 获取多个复选框的值
- PHP 获取同一个name多个复选框的值
- Android简单获取多个复选框的值
- 获取多个复选框值
- angular:如何获取多个复选框的id
- PHP获取多个同名复选框的值的处理(完整正确版本)
- PHP利用隐藏域获取多个同名复选框的值
- 获取复选框的值
- 获取复选框的值
- 获取复选框值
- 用jquery 获取多个复选框选项
- PHP 怎么接收多个复选框的值?
- 多个复选框的读取选中
- php获取复选框的值
- JavaScript获取复选框的值
- jQuery获取--checkbox复选框的值
- jquery获取复选框的值
- Hive内置运算符
- Android RxJava的基本使用三
- 上传图片
- eclipse 设置编译jdk版本及兼容版本
- ReactNative调试
- 获取多个复选框的值
- mysql数据库存储类型及存储引擎分析
- Android消息机制
- Windows任务计划 定时执行PHP代码及扩展(对数据库进行更新操作)
- angularjs+路由 实现用户列表
- ubuntu14.04 利用ros usb_cam包驱动摄像头后显示图像上下颠倒的解决方法
- Jq实现省市联动(JqDOM操作和遍历)
- 测试博客
- block copy字符修饰