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实现多选效果的更多相关文章

  1. android UI进阶之实现listview中checkbox的多选与记录

    今天继续和大家分享涉及到listview的内容.在很多时候,我们会用到listview和checkbox配合来提供给用户一些选择操作.比如在一个 清单页面,我们需要记录用户勾选了哪些条目.这个的实现并 ...

  2. 【转】android UI进阶之实现listview中checkbox的多选与记录--不错

    原文网址:http://www.cnblogs.com/notice520/archive/2012/02/17/2355415.html 今天继续和大家分享涉及到listview的内容.在很多时候, ...

  3. Android高级控件(一)——ListView绑定CheckBox实现全选,增加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,增加和删除等功能 这个控件还是挺复杂的,也是项目中应该算是比较常用的了,所以写了一个小Demo来讲讲,主要是自定义adap ...

  4. Android高级控件(一)——ListView绑定CheckBox实现全选,添加和删除等功能

    Android高级控件(一)--ListView绑定CheckBox实现全选,添加和删除等功能 这个控件还是挺复杂的.也是项目中应该算是比較经常使用的了,所以写了一个小Demo来讲讲,主要是自己定义a ...

  5. ListView+CheckBox实现全选 单击效果

    在网上也找了一些案例,但都是用Map来实现的.我的是把对象绑定到当前控件上.代码稍微简洁. main布局文件:main.xml <?xml version="1.0" enc ...

  6. Android 带checkbox的listView 实现多选,全选,反选,删除

    activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&qu ...

  7. Android 带checkbox的listView 实现多选,全选,反选

    由于listview的一些特性,刚开始写这种需求的功能的时候都会碰到一些问题,重点就是存储每个checkbox的状态值,在这里分享出了完美解决方法:     布局文件: [html]   <?x ...

  8. 【转】Android 带checkbox的listView 实现多选,全选,反选 -- 不错

    原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...

  9. 【转】Android 带checkbox的listView 实现多选,全选,反选----解决checkbox错位问题

    原文网址:http://blog.csdn.net/onlyonecoder/article/details/8687811 Demo地址(0分资源):http://download.csdn.net ...

随机推荐

  1. jquery实现的个人中心导航菜单

    之前为大家介绍了一款jquery和css3实现的很酷的菜单导航.这是一款由jquery开发的导航菜单.适合放在门户网站的个人用户中心后台.效果和美观都非常不错.我们先一起看看效果图: 在线预览   源 ...

  2. 【UE4】如何获取/下载虚幻4(Unreal Engine4)源码

    在官网中点击[获取虚幻引擎]可以看到,虚幻4完整源代码已经放在Github上,所以与其用百度搜别人的资源,当然是直接上Github下啊. 主要步骤如下: 注册一个Github帐号,这个没啥值得说的. ...

  3. SO_REUSEADDR的作用

    服务器socketstreamtcpc 原贴地址:http://topic.csdn.net/u/20090103/16/a0414edb-b289-4c72-84da-39e155e8f4be.ht ...

  4. iOS边练边学--(Quartz2D)图片添加水印

    一.给图片添加水印的基本步骤 加载图片 手动创建位图上下文 绘制原生的图片 给原生的图片添加文字 生成一张图片给我们,从上下文中获取图片 关闭上下文 二.注意:位图上下文的获取方式跟layer上下文不 ...

  5. XMLHttpRequest对象的常用属性与方法

    方法 一, open(); 书上解释: 用于设置请求的目标url请求方法, 以及其他参数信息 个人理解: 发送请求的页面在不刷新的情况能将参数传给一个服务器进行处理, 这个方法就是将这些个参数传送过去 ...

  6. (四)Qt实现自定义模型基于QAbstractTableModel (一般)

    Qt实现自定义模型基于QAbstractTableModel 两个例子 例子1代码 Main.cpp #include <QtGui> #include "currencymod ...

  7. 无限级分类 mysql设计

  8. 关于Cocos2d-x中字体的使用

    1.如果使用的是系统自带的 static Label* createWithSystemFont ( const std::string & text,        const std::s ...

  9. 【F12】修改 DevTools的主题

    1.点击setting

  10. Hibernate查询_HQL_EJBQL_QBC_QBE

    查询按功能强弱可以分为以下几种:Native SQL > HQL > EJBQL > QBC > QBE 1.Native SQLNative SQL为数据库系统本身的SQL, ...