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. 理解node的模板引擎

    1.1.3:分析模板引擎    1.什么是模板引擎 模板引擎是一个将页面模板和要显示的数据结合生成HTML页面的工具 可以这么理解,如果说Express中的路由控制方法是MVC中的控制器的话,那么模板 ...

  2. Sphinx coreseek 3.2

    功能 中文的拆词索引 MySQL中like模糊查询.   >1>5>%可以用索引 配置 用编辑器打开 path 配置 改绝对路径 https://blog.csdn.net/u013 ...

  3. 运行 svgatest 显示 mmap /dev/zero Permission denied 解决办法

    答案是我在这个网站上找到的: 执行 xset dpms force off 命令就可以解决掉这个问题. 再次运行 svgatest 程序,得到了预期的结果,perfect!

  4. Web前端基础——jQuery(一)

    前几天回老家呆了几天,几乎没有怎么学习新的知识,这期间一直有断断续续的看<Java编程思想>,还刷了一些前沿消息,也算没闲着.今天开始学习jQuery啦,继续前进. 在网上查了,买了这本书 ...

  5. GDB使用技巧

    最近使用GDB比较多,发现除了最常用的run.break.continue.next等命令的基本用法外,还有一些非常有用的命令和用法,能让你更加得心应手地使用GDB,在这里做了一下简单的总结. 1. ...

  6. Nodejs 和 Electron ubuntu下快速安装

    查找时间管理软件的时候发现了superProductivity这个程序,使用electron进行开发,于是看了一下介绍,手痒了,尝试进行环境搭建,下一步慢慢补齐前端知识吧 nodejs安装 nodej ...

  7. C语言字符串读入函数笔记

    gets(str)函数和scanf("%s",str)区别: 转自:https://zhidao.baidu.com/question/290403568.html 二者都是从终端 ...

  8. MVVM 和 VUE

    一,使用jquery和使用vue的区别 二,对MVVM的理解 三,vue中如何实现响应式 四,vue如何解析模版 五,vue整个实现流程   一,使用jquery和使用vue的区别 jquery实现t ...

  9. SVN SVN合并(Merge)与拉取分支(Branch/tag)操作简介

    SVN合并(Merge)与拉取分支(Branch/tag)操作简介 合并(Merge) 例子:把对feature_branch\project_name_v3.3.7_branch的修改合并到deve ...

  10. Python tab键命令补全

    pip install pyreadline import rlcompleter, readline readline.parse_and_bind('tab: complete') root@pe ...