main.xml:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<CheckBox
android:id="@+id/eatId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭" />
<CheckBox
android:id="@+id/sleepId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉" />
<CheckBox
android:id="@+id/playDotaId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打dota" />
<CheckBox
android:id="@+id/allCheckId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选" />
</LinearLayout>

MainActivity.java:

package com.example.allchecked;

import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity {
private CheckBox eatBox;
private CheckBox sleepBox;
private CheckBox playDotaBox;
private CheckBox allCheckBox; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
eatBox = (CheckBox)findViewById(R.id.eatId);
sleepBox = (CheckBox)findViewById(R.id.sleepId);
playDotaBox = (CheckBox)findViewById(R.id.playDotaId);
allCheckBox = (CheckBox)findViewById(R.id.allCheckId); AllCheckListener allCheckListener = new AllCheckListener();
allCheckBox.setOnCheckedChangeListener(allCheckListener); CheckBoxListener listener = new CheckBoxListener();
eatBox.setOnCheckedChangeListener(listener);
sleepBox.setOnCheckedChangeListener(listener);
playDotaBox.setOnCheckedChangeListener(listener); /* OnBoxClickListener clicklistener = new OnBoxClickListener();
eatBox.setOnClickListener(clicklistener);
sleepBox.setOnClickListener(clicklistener);
playDotaBox.setOnClickListener(clicklistener);
*/
} /* class OnBoxClickListener implements OnClickListener{ @Override
public void onClick(View view) {
// TODO Auto-generated method stub
CheckBox box = (CheckBox)view;
if(box.getId() == R.id.eatId){
System.out.println("eatBox");
}
else if(box.getId() == R.id.sleepId){
System.out.println("sleepBox");
}
else{
System.out.println("dotaBox");
}
if(box.isChecked()){
System.out.println("checked");
}
else{
System.out.println("unchecked");
}
}
}*/ public class CheckBoxListener implements OnCheckedChangeListener{ @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
if(buttonView.getId() == R.id.eatId){
System.out.println("eatBox");
}
else if(buttonView.getId() == R.id.sleepId){
System.out.println("sleepBox");
}
else{
System.out.println("dotaBox");
}
if(isChecked){
System.out.println("checked");
}
else{
System.out.println("unchecked");
}
} } public class AllCheckListener implements OnCheckedChangeListener{ @Override
public void onCheckedChanged(CompoundButton buttonView,
boolean isChecked) {
// TODO Auto-generated method stub
eatBox.setChecked(isChecked);
sleepBox.setChecked(isChecked);
playDotaBox.setChecked(isChecked);
} } }

全选按钮的功能还不是很完善,选中上面三个所有,全选按钮不会自动被选中;同时点击全选之后,点击上面某一个不再选中,全选不会自动取消选中。

最新文章

  1. ZoomEye 钟馗之眼 搜索工具 基于API
  2. 转: IntelliJ IDEA 2016.2.2注册码
  3. Linux系统1.md
  4. RMB转换人民币大小金额
  5. 【转】PHP 之 CURL 模拟登陆并获取数据
  6. Ztack学习笔记(4)-系统网络分析
  7. android 使用静态变量传递数据
  8. SAP存货账龄分析之库存获取
  9. Jquery 网站保存信息提示消息实现,提示后自动消失
  10. N年之后,只记得三井寿!而我们程序猿们也要加油珍惜时间!
  11. c++, 虚基派生 : 共同基类产生的二义性的解决办法
  12. ZOJ 3211 Dream City DP 01背包 经典问题
  13. ES 17 - (底层原理) Elasticsearch增删改查索引数据的过程
  14. sqlserver 存储过程 自定义函数 游标???
  15. JMeter上传案例2
  16. PowerDesigner 缺省值 引号 问题
  17. 【BUG】xml读取异常Invalid byte 1 of 1-byte UTF-8 sequence
  18. bzoj2243树链剖分+区间合并
  19. 安卓音、视频播放功能简单实现 --Android基础
  20. 记使用vue-awesome-swiper遇到的一些问题

热门文章

  1. Linux 笔记:路径
  2. PyQt5数据可视化
  3. centos 8 cockpit系统监控
  4. iOS 上通过 802.11k、802.11r 和 802.11v 实现 Wi-Fi 网络漫游
  5. 从零搭建vue+express开发环境
  6. Linux 下忘记mysql 密码
  7. js实现配置菜品规格时,向后台传一个json格式字符串
  8. nginx反向代理实战之轮询、Ip_hash、权重
  9. 07 DTFT
  10. Mybatis plus 插入数据时将自动递增的主键手动进行赋值设置