在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢

首先布局我们的界面:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"> <TextView
android:id="@+id/textviewtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="平时喜欢做什么事情?" /> <CheckBox
android:id="@+id/checkboxall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@id/textviewtitle"
android:layout_alignTop="@id/textviewtitle"
android:layout_toRightOf="@id/textviewtitle"
android:text="全选" />
<!--内容的CheckBox-->
<CheckBox
android:id="@+id/checkbox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle"
android:layout_marginRight="80dp"
android:text="玩游戏" /> <CheckBox
android:id="@+id/checkbox2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/textviewtitle"
android:layout_toRightOf="@+id/checkbox1"
android:text="学习" /> <CheckBox
android:id="@+id/checkbox3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox1"
android:text="敲代码" /> <CheckBox
android:id="@+id/checkbox4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox2"
android:layout_toRightOf="@+id/checkbox1"
android:text="跑步" /> <CheckBox
android:id="@+id/checkbox5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox3"
android:text="游泳" /> <CheckBox
android:id="@+id/checkbox6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox4"
android:layout_toRightOf="@+id/checkbox1"
android:text="睡觉" /> <TextView
android:id="@+id/textviewinfo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/checkbox5"
android:layout_marginTop="20dp"
android:text="已选择:"/>
</RelativeLayout>

注意要给对应的CheckBox编写Id,

接下来就是java代码了:代码主要实现的功能,点击全选按钮时,我会
把所有的选项选中,在全选的情况下,我取消任意一个复选框,那么全选按钮也会取消选中,同理,我选中了所有的复选框后,其全选按钮也会选中。

代码如下:

package com.dc.work3;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.TextView;
import android.widget.Toast; import java.util.LinkedList;
import java.util.List; /**
* Created by 丁先森
*/ public class MainActivity2s extends AppCompatActivity {
private CheckBox checkboxall;
private CheckBox checkBox1;
private CheckBox checkBox2;
private CheckBox checkBox3;
private CheckBox checkBox4;
private CheckBox checkBox5;
private CheckBox checkBox6; private TextView textviewinfo;
private List<String> checkedStr; //操作取消一个时,全选取消,这个变量是是否是用户点击
private boolean checkFoUser=true; protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_2); checkboxall = (CheckBox) findViewById(R.id.checkboxall);
checkBox1 = (CheckBox) findViewById(R.id.checkbox1);
checkBox2 = (CheckBox) findViewById(R.id.checkbox2);
checkBox3 = (CheckBox) findViewById(R.id.checkbox3);
checkBox4 = (CheckBox) findViewById(R.id.checkbox4);
checkBox5 = (CheckBox) findViewById(R.id.checkbox5);
checkBox6 = (CheckBox) findViewById(R.id.checkbox6);
textviewinfo = (TextView) findViewById(R.id.textviewinfo); checkBox1.setOnCheckedChangeListener(changeListener);
checkBox2.setOnCheckedChangeListener(changeListener);
checkBox3.setOnCheckedChangeListener(changeListener);
checkBox4.setOnCheckedChangeListener(changeListener);
checkBox5.setOnCheckedChangeListener(changeListener);
checkBox6.setOnCheckedChangeListener(changeListener);
checkboxall.setOnCheckedChangeListener(changeListener); checkedStr=new LinkedList<>(); }
public CompoundButton.OnCheckedChangeListener changeListener = new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
switch (buttonView.getId()){
case R.id.checkbox1:
case R.id.checkbox2:
case R.id.checkbox3:
case R.id.checkbox4:
case R.id.checkbox5:
case R.id.checkbox6:
String str=buttonView.getText().toString();
if(isChecked){
checkedStr.add(str);
}else {
checkedStr.remove(str);
}
// checkFoUser=false;
checkboxall.setOnCheckedChangeListener(null);
if(checkBox1.isChecked()&&checkBox2.isChecked()&&checkBox3.isChecked()&&checkBox4.isChecked()&&checkBox5.isChecked()&&checkBox6.isChecked()){
//表示如果都选中时,把全选按钮也选中
checkboxall.setChecked(true);
}else {
//否则就全选按钮去不选中,但是这样会触发checkboxall的监听,会把所有的都取消掉
checkboxall.setChecked(false);
}
// checkFoUser=true;
/*第二种方法*/
checkboxall.setOnCheckedChangeListener(changeListener);
break;
case R.id.checkboxall:
if(checkFoUser) {
checkBox1.setChecked(isChecked);
checkBox2.setChecked(isChecked);
checkBox3.setChecked(isChecked);
checkBox4.setChecked(isChecked);
checkBox5.setChecked(isChecked);
checkBox6.setChecked(isChecked);
break;
}
}
StringBuffer sb=new StringBuffer();
for(String str:checkedStr){
sb.append(str+",");
}
if(sb.length()>0){
//设置长度为长度-1,去除最后的“,”
sb.setLength(sb.length()-1);
}
textviewinfo.setText("已选择:"+sb.toString());
}
}; }
写的不是很好,但是可以简单的解决问题,欢迎大家积极交流,留下宝贵的建议,共同学习!

Android开发CheckBox控件,全选,反选,取消全选的更多相关文章

  1. CAD控件,CAD插件使用教程:Android开发使用控件--开发环境的搭建

    Android开发使用控件入门--环境搭建 2014-12-24 09:57     14人阅读     评论(0)     收藏         编辑     删除 CAD控件.CAD三维控件,手机 ...

  2. Android开发使用控件入门--环境搭建

    Android开发使用控件入门--环境搭建 软件名称(,梦,,想.CAD  ,控件) 1. 环境搭建: 3 1.1. 安装Eclipse 3 1.2. 下载JDK 3 1.3. 下载Android S ...

  3. Android开发ImageView控件缩放图片

    首先还是最基础的ImageView控件如何显示图片: <ImageView                Android:id="@+id/imgView"          ...

  4. android 开发-设置控件/view的水平方向翻转

    设置控件沿着水平方向翻转(即Y轴180°) 看效果: 代码: <pl.droidsonroids.gif.GifImageView android:id="@+id/gv_image1 ...

  5. android 开发 ScrollView 控件的一些api描述与自定义ScrollView接口回调方法

    1.正常使用ScrollView控件的一些api详解. package com.example.lenovo.mydemoapp.scrollViewDemo; import android.supp ...

  6. 【Android开发】控件外边框自定义

    1.在drawable里面新建自定义的资源文件shape <?xml version="1.0" encoding="utf-8"?> <sh ...

  7. 从零开始学android开发-获取控件

    mBtnNews = (Button)findViewById(R.id.btn_news);//获取控件

  8. Android:CheckBox控件

    1)ChexkBox继承自CompoundButton组件: 2)isChecked()--确定是否选中:setChecked(bool checked)--设置选中或取消选中: 3)监听事件:Com ...

  9. Android 开发添加控件事件的三种方式

    import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view ...

随机推荐

  1. PyTorch-Adam优化算法原理,公式,应用

    概念:Adam 是一种可以替代传统随机梯度下降过程的一阶优化算法,它能基于训练数据迭代地更新神经网络权重.Adam 最开始是由 OpenAI 的 Diederik Kingma 和多伦多大学的 Jim ...

  2. mysql 开发进阶篇系列 21 磁盘I/O问题(RAID)

    一.概述 作为应用系统的持久化层,不管数据库采取了什么样的Cache机制,数据库最终总是要将数据储存到可以长久保存的I/O设备磁盘上.但磁盘的存取速度显然要比cpu,ram的速度慢很多.因此,对于比较 ...

  3. Netdata 服务器前端监控平台

    Netdata 是一款 Linux 性能实时监测工具.Netdata是Linux系统实时性能监测工具,提供web界面的界面视角. 它用可视化的手段,将被监测者最细微的细节,展现了出来.这样,你便可以清 ...

  4. Python快速学习03:运算 & 缩进和选择

    前言 系列文章:[传送门] 这篇昨晚本来要出的,去搭了帐篷,在学校的屋顶上. 运算 运算,不得不说的是运算符. 数学 +, -, *, /, **, %,// 判断 ==, !=, >, > ...

  5. 如何用vue-router为每个路由配置各自的title

    传统方法 以前在单页面路由中,就只能在html文件中定一个固定的网站的title.如果想要动态的去修改,需要使用如下的方法. document.title = '这是一个标题'; 这样会让我们做很多无 ...

  6. 【干货】利用MVC5+EF6搭建博客系统(四)(上)前后台页面布局页面实现,介绍使用的UI框架以及JS组件

    一.博客系统进度回顾以及页面设计 1.1页面设计说明 紧接前面基础基本完成了框架搭建,现在开始设计页面,前台页面设计我是模仿我博客园的风格来设计的,后台是常规的左右布局风格. 1.2前台页面风格 主页 ...

  7. 一个案例彻底弄懂如何正确使用 mysql inndb 联合索引

    有一个业务是查询最新审核的5条数据 SELECT `id`, `title` FROM `th_content` WHERE `audit_time` < 1541984478 AND `sta ...

  8. Perl的特殊代码块:BEGIN、CHECK、INIT、END和UNITCHECK

    这是5个特殊的代码块.要理解这几个块,关键在于几个时间点: (1).程序编译期间 (2).程序执行期间 (3).程序执行结束但还未退出期间 BEGIN块 BEGIN块是在程序编译期间执行的,也就是上面 ...

  9. [转]ASP.NET Core: Static Files cache control using HTTP Headers

    本文转自:https://www.ryadel.com/en/asp-net-core-static-files-cache-control-using-http-headers/ Every sea ...

  10. [转]windows BLE编程 net winform 连接蓝牙4.0

    本文转自:https://www.cnblogs.com/webtojs/p/9675956.html winform 程序调用Windows.Devices.Bluetoot API 实现windo ...