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 ...
随机推荐
- iOS UIWebView与WKWebView使用详解
一.整体介绍 UIWebView自iOS2就有,WKWebView从iOS8才有,毫无疑问WKWebView将逐步取代笨重的UIWebView.通过简单的测试即可发现UIWebView占用过多内存,且 ...
- 【C#/WPF】图像变换的Undo撤销——用Stack命令栈
需求: 图层中有一张图片,可以对该图层进行平移.缩放.旋转操作,现在要求做Undo撤销功能,使得图层回复上一步操作时的状态. 关于图像的平移.缩放.旋转,可以参考在下的另一篇博客的整理: http:/ ...
- 带命名空间的XML的dom4j应用<转>
Element root = document.getRootElement(); List recordenvlist = document.selectNodes("//gm ...
- linux下ppp拨号无线上网
linux下用ppp上网需要两个程序:pppd和chat.ubuntu自带pppd和chat,可以使用man查看具体使用方法. 典型的ppp拨号需要准备几个文件: 1. pppd脚本. 2. chat ...
- Java上的jQuery?解析HTML利器—Jsoup
也许大家有过在java运行平台上解析html的经历,通常的方式是将HTML以XML的形式进行结点解析,调用java本身的xml解析类库.这样的方式很容易理解并且很方便,但习惯用jQuery的各位是否在 ...
- Windows 2008 Server搭建Radius服务器的方法
原地址:http://service.tp-link.com.cn/detail_article_1113.html (图拷贝不过来) Windows 2008 Server搭建Radius服务器的方 ...
- lua——string之string.gsub
translated from the lua document string.gsub用法: 函数原型:string.gsub( s, pattern, rep1[, n] ) 函数功能:返回一个和 ...
- /etc/fstab下的挂载类型defaults默认参数
[root@ob2 data]# cat /etc/fstab -98c0-59dbbcf8b040 / ext4 defaults UUID=e4ab7a0c-500f--bcd2-a77be0ce ...
- find 命令一个命令多参数如何使用,????,perm
[root@ob2 mytmp]# find -mtime -7 -type f \( -name "*.html" -o -name "*.tar.gz" \ ...
- CodeIgniter(3.1.4)框架中添加执行时间统计代码
CodeIgniter(3.1.4)框架中添加,执行时间统计代码: system/core/CodeIgniter.php最后行处. /* * ---------------------------- ...