Android 使用CheckBox实现多选效果
CheckBox:复选框
1.有两种状态:
选中状态(true),未选中状态(false)
2.属性:
android:id="@+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:text="男"
CheckBox的默认android:checked属性为false。
checkBox的OnCheckedChangeListener事件检查勾是否勾选。
样例程序中有3个CheckBox和1个TextView,TextView事实演示了有多少CheckBox被勾选了以及被勾选的CheckBox的名称。
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="篮球" /> <CheckBox
android:id="@+id/checkBox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="足球" /> <CheckBox
android:id="@+id/checkBox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="乒乓球" /> <TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0项选择" /> </LinearLayout>
activity_main.xml
package com.example.checkbox; import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener;
import android.widget.TextView; public class MainActivity extends ActionBarActivity { private CheckBox basketballCheckBox;
private CheckBox footballCheckBox;
private CheckBox pingpongCheckBox; private boolean[] checkedArray = new boolean[] {false, false, false}; private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); basketballCheckBox = (CheckBox) findViewById(R.id.checkBox1);
footballCheckBox = (CheckBox) findViewById(R.id.checkBox2);
pingpongCheckBox = (CheckBox) findViewById(R.id.checkBox3);
textView = (TextView) findViewById(R.id.textView1); basketballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedArray[0] = isChecked;
textViewResetValue();
}
});
footballCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedArray[1] = isChecked;
textViewResetValue();
}
});
pingpongCheckBox.setOnCheckedChangeListener(new OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
checkedArray[2] = isChecked;
textViewResetValue();
}
}); } private void textViewResetValue() {
String values = "";
int sumChecked = 0;
for (boolean val : checkedArray)
if (val == true)
sumChecked += 1;
if (sumChecked == 0)
textView.setText("0 项选择");
else {
if (checkedArray[0] == true) values += ",篮球";
if (checkedArray[1] == true) values += ",足球";
if (checkedArray[2] == true) values += ",乒乓球";
values = sumChecked + "项选择:" + values.substring(1);
textView.setText(values);
}
}
}
MainActivity.java
效果:

注:Android有一个自己的log记录函数:Log.i()。
Android 使用CheckBox实现多选效果的更多相关文章
- android UI进阶之实现listview中checkbox的多选与记录
今天继续和大家分享涉及到listview的内容.在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作.比如在一个 清单页面,我们需要记录用户勾选了哪些条目.这个的实现并 ...
- 【转】android UI进阶之实现listview中checkbox的多选与记录--不错
原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...
- Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能
Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...
- Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能
Android高级控件(一)--ListView绑定CheckBox实现全选,添加和删除等功能 这个控件还是挺复杂的.也是项目中应该算是比較经常使用的了,所以写了一个小Demo来讲讲,主要是自己定义a ...
- ListView+CheckBox实现全选 单击效果
在网上也找了一些案例,但都是用Map来实现的.我的是把对象绑定到当前控件上.代码稍微简洁. main布局文件:main.xml <?xml version="1.0" enc ...
- Android 带checkbox的listView 实现多选,全选,反选,删除
activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...
- Android 带checkbox的listView 实现多选,全选,反选
由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法: 布局文件: [html] <?x ...
- 【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错
原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...
- 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题
原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...
随机推荐
- JS地毯式学习一
1.<noscript> 现代浏览器都对JavaScript进行了支持,一般是在用户的浏览器禁用了脚本的情况下才会显示<noscript>的内容. 包含在<noscrip ...
- plot sin 03-数据区域边界线的位置
plot sin 03 数据区域边界线的位置 Code #!/usr/bin/env python # -*- coding: utf-8 -*- import numpy as np import ...
- u-boot bootz 加载kernel 流程分析
u-boot 加载 kernel 的流程分析. image重要结构体头文件 // include/image.h * * Legacy and FIT format headers used by d ...
- kubernetes deployment
deployment是k8s中部署应用最常见的一种方式.如果不需要被访问,那么只需要定义deployment即可.如果需要被其他服务访问,那么可以创建一个service与其绑定,通过访问service ...
- debian配置ftp
大家好,最近几天我在配置vsftpd,总结出如何更快的配置vsftpd1.我的系统是debian 5.02.安装 vsftpd, apt-get install vsftpd3.配置 vsftpdcd ...
- html中的label配合checkbox,redio用法
<input id="a1" type="checkbox" name="a" value="33023" /&g ...
- GOF23设计模式汇总
转自:http://www.cnblogs.com/zhili/p/DesignPatternSummery.html#3037698 C#设计模式总结 一.引言 经过这段时间对设计模式的学习,自己的 ...
- Java泛型函数的运行时类型检查的问题
在一个数据持久化处理中定义了数据保存和读取的 泛型函数的,但是在运行时出现类型转换错误,类型不匹配,出错的位置不是load方法,而是在调用load方法之后,得到了列表数据,对列表数据进行使用时出现的. ...
- C++中 char *s 和 char s[] 的区别
原因 刚好看到给main传递参数,书上(C++Primer)说“ int main(int argc, char *argv[])也可以写成 int main(int argc, char **arg ...
- 更改windows 2003远程桌面端口3389为其他的端口号【转】
众所周知,windows 2003远程终端服务基于默认端口3389.入侵者一般先扫描主机开放端口,一旦发现其开放了3389端口,就会进行下一步的入侵,所以我们只需要修改该务默认端口就可以避开大多数入侵 ...