import android.os.Bundle;
import android.app.Activity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity {
private CheckBox swimBox = null;
private CheckBox runBox = null;
private CheckBox readBox = null;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); swimBox = (CheckBox) findViewById(R.id.swim);
runBox = (CheckBox) findViewById(R.id.run);
readBox = (CheckBox) findViewById(R.id.read); CheckBoxListener listener = new CheckBoxListener();
swimBox.setOnCheckedChangeListener(listener);
runBox.setOnCheckedChangeListener(listener);
readBox.setOnCheckedChangeListener(listener);
/*
//多个控件可以使用同一个监听器
OnBoxClickListener listener = new OnBoxClickListener();
swimBox.setOnClickListener(listener);
runBox.setOnClickListener(listener);
readBox.setOnClickListener(listener);*/
} //
class CheckBoxListener implements OnCheckedChangeListener{
@Override
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {
if(buttonView.getId()==R.id.read){
System.out.println("read");
}else if(buttonView.getId()==R.id.run){
System.out.println("run");
}else if(buttonView.getId()==R.id.swim){
System.out.println("swim");
}
if(isChecked){
System.out.println("Checked");
}else{
System.out.println("UnChecked");
}
} } /* OnClickListener的使用方法
class OnBoxClickListener implements OnClickListener{
public void onClick(View view) { //CheckBox是View的子类,所以可以接收
CheckBox box = (CheckBox)view;
if(view.getId()==R.id.read)
System.out.println("read");
else if(view.getId()==R.id.run)
System.out.println("run");
else if(view.getId()==R.id.swim)
System.out.println("swim"); if(box.isChecked()){//isChecked方法不是view中的方法所以要向下转型,该方法可以判段是否被选中,如果选中返回真
System.out.println("Checked");
}else
System.out.println("unChecked");
} }
*/
}
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
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/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打游戏"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉"
/>
</LinearLayout>

实现全选功能:

 import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends Activity {
private CheckBox all;
private CheckBox swim;
private CheckBox run;
private CheckBox read;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); all = (CheckBox) findViewById(R.id.all);
swim = (CheckBox) findViewById(R.id.swim);
run = (CheckBox) findViewById(R.id.run);
read = (CheckBox) findViewById(R.id.read); AllCheckListener listener = new AllCheckListener();
all.setOnCheckedChangeListener(listener);
}
class AllCheckListener implements OnCheckedChangeListener{
public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) { swim.setChecked(isChecked);
run.setChecked(isChecked);
read.setChecked(isChecked);
/*
if(isChecked){
swim.setChecked(true);
run.setChecked(true);
read.setChecked(true);
}else{
swim.setClickable(false);
run.setChecked(false);
read.setChecked(false);
}
*/
} } }
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
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/all"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="全选"
/> <CheckBox
android:id="@+id/swim"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吃饭"
/>
<CheckBox
android:id="@+id/run"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="打游戏"
/>
<CheckBox
android:id="@+id/read"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉"
/>
</LinearLayout>

控件_CheckBox(多选按钮)的更多相关文章

  1. 1如何给devexpress的gridview控件绘制全选按钮

    1 首先注册gridview的this.edibandedGridView.CustomDrawColumnHeader += EdibandedGridView_CustomDrawColumnHe ...

  2. Android开发CheckBox控件,全选,反选,取消全选

    在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...

  3. CTreeCtrl 控件实现多选并取得选中项

    刚开始以为实现起来很难,所以就在网上寻找实现的扩展控件,到最后才发现只要把CTreeCtrl 控件的Check Boxes 属性设为真就可以了,会在每个树形节点前添加一个CheckBox. 多选已经实 ...

  4. Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子

    Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...

  5. Android 5.0新控件——FloatingActionButton(悬浮按钮)

    Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...

  6. ASP.NET CheckBoxList 控件实现全选、反选、清除功能 利用js

    直接看代码: JS代码如下: <script type="text/javascript" language="javascript"> funct ...

  7. VS2010中新控件的编程------颜色按钮类和颜色对话框

    (1)      颜色按钮类和颜色对话框 1) 颜色对话框 MFC提供了颜色对话框类CMFCColorDialog进行颜色的选择,系统可以利用DoModal()调用,然后选择相应的颜色. CMFCCo ...

  8. Winform中使用DevExpress的CheckEdit控件实现多选条件搜索

    场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...

  9. Android控件之Button(按钮控件)和ImageButton(图片按钮控件)

    一.Button和ImageButton特证: 1.共同特证: 都可以作为一个按钮产生点击事件 2.不同特证: Button有text的属性,ImageButton没有 ImageButton有src ...

随机推荐

  1. ProxySQL Cluster 高可用集群环境部署记录

    ProxySQL在早期版本若需要做高可用,需要搭建两个实例,进行冗余.但两个ProxySQL实例之间的数据并不能共通,在主实例上配置后,仍需要在备用节点上进行配置,对管理来说非常不方便.但是Proxy ...

  2. 搭建前端监控系统(四)Js截图上报篇

    ===================================================================== 前端监控系统: DEMO地址  GIT代码仓库地址 ==== ...

  3. ElasticSearch入门简介

    ElasticSearch是基于Apache Lucene的分布式搜索引擎, 提供面向文档的搜索服务.本文以6.2.3版本为例介绍ElasticSearch的应用. 本文首先介绍ElasticSear ...

  4. spring boot(二): spring boot+jdbctemplate+sql server

    前言 小项目或者做demo时可以使用jdbc+sql server解决即可,这篇就基于spring boot环境使用jdbc连接sql server数据库,和spring mvc系列保持一致. 在sp ...

  5. vue 使用mint-ui实现上拉加载和下拉刷新

    解决了官网中下拉刷新存在的问题 <template> <div class="tmpl"> <nav-bar title="商品列表&quo ...

  6. sql特殊语法

    MYSQL --判断非空select ifnull(null,'666');--666select ifnull(null,null);--null--合并字段select CONCAT('666', ...

  7. 【Java】模拟Sping,实现其IOC和AOP核心(二)

    接着上一篇,在上一篇完成了有关IOC的注解实现,这一篇用XML的方式实现IOC,并且完成AOP. 简易的IOC框图 注解的方式实现了左边的分支,那么就剩下右边的XML分支: XmlContext:这个 ...

  8. JavaSE Map集合

    Map集合 在Map集合中保存的数据为一组数据,其中:一个数据为key,另外一个数据为value.而key和value具备对应的关系,在集合中它们属于一组(一对)数据.而每个key只能对应唯一的一个v ...

  9. Java基础回顾Application(二)

    application对象 1 什么是application对象 ? (1) 当Web服务器启动时,Web服务器会自动创建一个application对象.application对象一旦创建,它将一直存 ...

  10. Linux常用基本命令:三剑客命令之-awk基础用法

    awk是一个超级强大的文本格式化处理工具,他与grep, sed命令被成为linux 三剑客命令 三剑客命令的特点: grep:只要用来匹配和查找文本 sed: 编辑匹配到文本 awk: 格式化文本, ...