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. Hive基础之Hive与关系型数据库的比较

    Hive与关系型数据库的比较     使用Hive的CTL(命令行接口)时,你会感觉它很像是在操作关系型数据库,但是实际上,Hive和关系型数据库有很大的不同.       1)Hive和关系型数据库 ...

  2. [HAOI 2016]找相同字符

    Description 题库链接 给定两个只含小写字母字符串 \(s_1,s_2\) ,求出在两个字符串中各取出一个子串使得这两个子串相同的方案数.两个方案不同当且仅当这两个子串中有一个位置不同. \ ...

  3. 常用的NoSQL数据库类型简述

    一.文档存储类型(Document Stores) 文档存储,也称为面向文档的数据库系统,其主要特点在于它们的无模式的数据组织. 特点: 1.记录数据不需要具有统一的结构,即不同的记录可以具有不同的列 ...

  4. .8-浅析webpack源码之Tapable介绍

    Tapable工具 完成webpack默认参数注入后,下一步虽然是 new Compiler() ,但是这东西不是一下可以讲完的,复杂的一批. 不如先从工具入手,分块讲解compiler,首先来看看事 ...

  5. [引]ionic framework 相关网站

    http://www.w3uu.com/intro/mobile/ionic2-tutorial.html  ionic2中文教程 https://beta.ionicframework.com/do ...

  6. Redis管道

    介绍 Redis是一种基于客户端-服务端模型以及请求/响应协议的TCP服务.客户端请求会遵循以下步骤:客户端向服务端发送一个查询请求,并监听Socket返回,通常是以阻塞模式,等待服务端响应并将结果返 ...

  7. 转STM32官方固件库简介

    ST(意法半导体)为了方便用户开发程序,提供了一套丰富的 STM32 固件库.固件库就是函数的集合,固件库函数的作用是向下负责与寄存器直接打交道,向上提供用户函数调用的接口(API) .固件库将这些寄 ...

  8. Oracle索引失效原因及解决方法

    一.Oracle索引失效的原因 1使用否定关键字 !=, <> ,not in,not exist select * fromdrama where id <> 1,Mysql ...

  9. Matlab arenstorf problem

    % right-hand side of arenstorf problem function yDot = arenstorf(t,y) global mu muHat % unpack y u1 ...

  10. 聊聊大麦网UWP版的首页顶部图片联动效果的实现方法

    随着Windows10的发布,国内已经有越来越多的厂商上架了自家的通用应用程序客户端,比如QQ.微博.大麦等.所实话,他们设计的确实很好,很符合Windows10 的设计风格和产品理念,而对于开发者而 ...