控件_CheckBox(多选按钮)
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如何给devexpress的gridview控件绘制全选按钮
1 首先注册gridview的this.edibandedGridView.CustomDrawColumnHeader += EdibandedGridView_CustomDrawColumnHe ...
- Android开发CheckBox控件,全选,反选,取消全选
在Android开发中我们经常会使用CheckBox控件,那么怎么实现CheckBox控件的全选,反选呢 首先布局我们的界面: <?xml version="1.0" enc ...
- CTreeCtrl 控件实现多选并取得选中项
刚开始以为实现起来很难,所以就在网上寻找实现的扩展控件,到最后才发现只要把CTreeCtrl 控件的Check Boxes 属性设为真就可以了,会在每个树形节点前添加一个CheckBox. 多选已经实 ...
- Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子
Delphi7 第三方控件1stClass4000的TfcImageBtn按钮控件动态加载jpg图片例子 procedure TForm1.Button1Click(Sender: TObject); ...
- Android 5.0新控件——FloatingActionButton(悬浮按钮)
Android 5.0新控件--FloatingActionButton(悬浮按钮) FloatingActionButton是5.0以后的新控件,一个悬浮按钮,之所以叫做悬浮按钮,主要是因为自带阴影 ...
- ASP.NET CheckBoxList 控件实现全选、反选、清除功能 利用js
直接看代码: JS代码如下: <script type="text/javascript" language="javascript"> funct ...
- VS2010中新控件的编程------颜色按钮类和颜色对话框
(1) 颜色按钮类和颜色对话框 1) 颜色对话框 MFC提供了颜色对话框类CMFCColorDialog进行颜色的选择,系统可以利用DoModal()调用,然后选择相应的颜色. CMFCCo ...
- Winform中使用DevExpress的CheckEdit控件实现多选条件搜索
场景 Winform控件-DevExpress18下载安装注册以及在VS中使用: https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/1 ...
- Android控件之Button(按钮控件)和ImageButton(图片按钮控件)
一.Button和ImageButton特证: 1.共同特证: 都可以作为一个按钮产生点击事件 2.不同特证: Button有text的属性,ImageButton没有 ImageButton有src ...
随机推荐
- #6 Python数据类型及运算
前言 前文讲述了Python的输入输出以及变量的相关知识点,本节将探讨Python的数据类型以及数据之间的运算方式! 一.Python数据类型 上一节弄清了变量,其实变量所指向的值是有自己独特的数据类 ...
- Java设计模式学习记录-责任链模式
前言 已经把五个创建型设计模式和七个结构型设计模式介绍完了,从这篇开始要介绍行为型设计模式了,第一个要介绍的行为型设计模式就是责任链模式(又称职责链模式). 责任链模式 概念介绍 责任链模式是为了避免 ...
- 自动化运维(2)之一键式单实例安装MySQL
ZMySQLAutoTools文档 目标:自动化构建部署MySQL数据库,一键式单实例mysql安装,备份,监控,主从集群部署等.以及jdk,tomcat,nginx等基础中间件的自动化部署安装及运维 ...
- [转]Entity Framework and slow bulk INSERTs
本文转自:https://weblog.west-wind.com/posts/2013/Dec/22/Entity-Framework-and-slow-bulk-INSERTs I’ve been ...
- Net 如何计算一段代码的效率
在.Net 4.0以后的版本,提供了一个类,该类在 System.Diagnostics命名空间下,使用该类就可以计算出执行结果相同的两端代码的效率,在代码优化上是很实用的. 泛型效率是高是低呢??我 ...
- 如何靠谱地查到Tomcat的版本
Tomcat版本获取 一般找jdk的版本的时候,我们直接执行如下命令就可以得知了 java -version 但是Tomcat的版本呢? 除了Tomcat安装目录路径里包含的版本号,还有其他靠谱的获取 ...
- Apache SkyWalking的架构设计【译文】
Apache SkyWalking提供了一个功能强大并且很轻量级的后端.在此,将介绍为什么采用以下方式来设计它,以及它又是如何工作的. 架构图 对于APM而言,agent或SDKs仅是如何使用libs ...
- 微信跳一跳Python辅助无需配置一键操作
作者:NiceCui 本文谢绝转载,如需转载需征得作者本人同意,谢谢. 本文链接:http://www.cnblogs.com/NiceCui/p/8350329.html 邮箱:moyi@moyib ...
- echarts环形图,自定义说明文字
一.代码 app.title = '已安装通讯盒电站统计'; option = { backgroundColor: '#0f0f31',//#0f0f31 title: { show:true, x ...
- PHP7.27: connect mysql 5.7 using new mysqli
<!doctype html> <html> <head> <meta name="viewport" content="wid ...