今天我们介绍的是Checkbox多选框:

1.Activity

//复选框,[基础控件]---状态切换控件CompoundButton及其子类CheckBox、RadioButton、ToggleButton、switch事件监听与场景使用
public class CheckBoxActivity extends Activity implements CompoundButton.OnCheckedChangeListener{ private Context context;
private CheckBox sleepCheckBox;
private CheckBox dadoudouCheckBox;
private CheckBox gameCheckBox;
private CheckBox shoppingCheckBox;
private CheckBox filmCheckBox;
private CheckBox sportCheckBox;
private Button submitButton; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.check_box); init();
addAction(); } private void init(){
context = this;
sleepCheckBox = (CheckBox)findViewById(R.id.sleepCheckBoxId);
dadoudouCheckBox = (CheckBox)findViewById(R.id.dadoudouCheckBoxId);
gameCheckBox = (CheckBox)findViewById(R.id.gameCheckBoxId);
shoppingCheckBox = (CheckBox)findViewById(R.id.shoppingCheckBoxId);
filmCheckBox = (CheckBox)findViewById(R.id.filmCheckBoxId);
sportCheckBox = (CheckBox)findViewById(R.id.sportCheckBoxId);
submitButton = (Button)findViewById(R.id.submitButtonId);
} private void addAction(){
sleepCheckBox.setOnCheckedChangeListener(this);
dadoudouCheckBox.setOnCheckedChangeListener(this);
gameCheckBox.setOnCheckedChangeListener(this);
shoppingCheckBox.setOnCheckedChangeListener(this);
filmCheckBox.setOnCheckedChangeListener(this);
sportCheckBox.setOnCheckedChangeListener(this);
submitButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
//String 字符串常量
//StringBuffer 字符串变量(线程安全)
//StringBuilder 字符串变量(非线程安全)
StringBuilder sb = new StringBuilder("您的兴趣是:");
//MyStringBuilder.Insert(6,"Beautiful ");
//MyStringBuilder.Remove(5,7);
//MyStringBuilder.Replace('!', '?');
//代码示例指定可以将 MyStringBuilder对象扩充到最大 25个空白。
//StringBuilderMyStringBuilder = new StringBuilder("Hello World!", 25);
if(sleepCheckBox.isChecked()){
sb.append("睡觉 ");
}
if(dadoudouCheckBox.isChecked()){
sb.append("打豆豆 ");
}
if(gameCheckBox.isChecked()){
sb.append("游戏 ");
}
if(shoppingCheckBox.isChecked()){
sb.append("购物 ");
}
if(filmCheckBox.isChecked()){
sb.append("电影 ");
}
if(sportCheckBox.isChecked()){
sb.append("运动");
} Toast.makeText(context, sb.toString(), Toast.LENGTH_LONG).show();
}
});
} @Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
int id = buttonView.getId();
switch(id){
case R.id.sleepCheckBoxId:
if(isChecked){
Toast.makeText(context, "你选择了\"睡觉\"", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "你取消选择了\"睡觉\"", Toast.LENGTH_SHORT).show();
}
break;
case R.id.dadoudouCheckBoxId:
if(isChecked){
Toast.makeText(context, "你选择了\"打豆豆\"", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "你取消选择了\"打豆豆\"", Toast.LENGTH_SHORT).show();
}
break;
case R.id.gameCheckBoxId:
if(isChecked){
Toast.makeText(context, "你选择了\"游戏\"", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "你取消选择了\"游戏\"", Toast.LENGTH_SHORT).show();
}
break;
case R.id.shoppingCheckBoxId:
if(isChecked){
Toast.makeText(context, "你选择了\"购物\"", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "你取消选择了\"购物\"", Toast.LENGTH_SHORT).show();
}
break;
case R.id.filmCheckBoxId:
if(isChecked){
Toast.makeText(context, "你选择了\"电影\"", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "你取消选择了\"电影\"", Toast.LENGTH_SHORT).show();
}
break;
case R.id.sportCheckBoxId:
if(isChecked){
Toast.makeText(context, "你选择了\"运动\"", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "你取消选择了\"运动\"", Toast.LENGTH_SHORT).show();
}
break;
}
} }

2.xml文件如下:

<?xml version="1.0" encoding="utf-8"?>
<!-- 复选框页面 -->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="5dp" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="兴趣:"
android:textSize="20sp" /> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <CheckBox
android:id="@+id/sleepCheckBoxId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="睡觉" /> <CheckBox
android:id="@+id/dadoudouCheckBoxId"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:text="打豆豆" /> <CheckBox
android:id="@+id/gameCheckBoxId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="游戏" />
</LinearLayout> <LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" > <CheckBox
android:id="@+id/shoppingCheckBoxId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="购物" /> <CheckBox
android:id="@+id/filmCheckBoxId"
android:layout_width="85dp"
android:layout_height="wrap_content"
android:text="电影" /> <CheckBox
android:id="@+id/sportCheckBoxId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="运动" />
</LinearLayout> <Button
android:id="@+id/submitButtonId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:text="确定"
android:textSize="20sp" /> </LinearLayout>

3.效果图如下:

多选按钮(CheckBox)的更多相关文章

  1. JQuery 多选按钮checkbox

    JQuery 多选按钮checkbox 在需要全选和选择部分的时候我们就需要多选在这里主要介绍了具体的实现 JQuery $(function () { //全选或全不选 $("#allbo ...

  2. 多选按钮CheckBox

    main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...

  3. cocos2d-x JS 复选按钮checkBox的单选与多选

    var HZ_createRoom = jx.BaseView.extend({//红中麻将 ctor : function() { this._super(); this.addLayout(res ...

  4. zepto全选按钮之全选会根据按钮是否被全部选中更改状态

    在做手机端二次开发购物车的时候,发现zepto全选,没找到,或者功能不是自己想要的 后来做好,分享给需要的人 //全选或多选处理      var CheckAll = $('#items_check ...

  5. Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关

    Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...

  6. 『心善渊』Selenium3.0基础 — 14、Selenium对单选和多选按钮的操作

    目录 1.页面中的单选按钮和多选按钮 2.判断按钮是否选中is_selected() 3.单选按钮的操作 4.多选按钮的操作 5.选择部分多选按钮的操作 1.页面中的单选按钮和多选按钮 页面中的单选按 ...

  7. checkbox做全选按钮

    1.先写一个html页面,里面写一个全选按钮和几个复选框,实现下面2个要求 (1)点击全选按钮选中时,所有的复选框选中. (2)点击全选按钮取消选中时,所有复选框取消选中. <input typ ...

  8. js做全选,用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false

    用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false,当所有checkbox都被选中时,全选按钮也被选中. 详解: 有两种 ...

  9. 安卓开发_复选按钮控件(CheckBox)的简单使用

    复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈 复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性 一.由于 ...

  10. 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义

    参考文章: 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义

随机推荐

  1. css3,background-clip/background-origin的使用场景,通俗讲解

    先不说background-clip/background-origin的用法,我们先来聊聊css背景方面的知识. <!DOCTYPE html> <html lang=" ...

  2. LeetCode-96. Unique Binary Search Trees

    Description: Given n, how many structurally unique BST's (binary search trees) that store values 1.. ...

  3. android基础---->JSON数据的解析

    上篇博客,我们谈到了XML两种常用的解析技术,详细可以参见我的博客(android基础---->XMl数据的解析).网络传输另外一种数据格式JSON就是我们今天要讲的,它是比XML体积更小的数据 ...

  4. Hadoop下面WordCount运行详解

    单词计数是最简单也是最能体现MapReduce思想的程序之一,可以称为MapReduce版"Hello World",该程序的完整代码可以在Hadoop安装包的"src/ ...

  5. Python3.x中bytes类型和str类型深入分析

    Python 3最重要的新特性之一是对字符串和二进制数据流做了明确的区分.文本总是Unicode,由str类型表示,二进制数据则由bytes类型表示.Python 3不会以任意隐式的方式混用str和b ...

  6. Java魔法堂:String.format详解

      目录     一.前言    二.重载方法     三.占位符     四.对字符.字符串进行格式化     五.对整数进行格式化     六.对浮点数进行格式化     七.对日期时间进行格式化 ...

  7. Struts 2 拦截器

    什么是Struts 2 拦截器  拦截器就是当用户请求后台Action类时在Action的Excute()方法执行前和Result返回魔板试图之后(将页面(数据)发送给浏览器渲染之前)所需要的一些通用 ...

  8. 用Qt写软件系列三:一个简单的系统工具之界面美化

    前言 在上一篇中,我们基本上完成了主要功能的实现,剩下的一些导出.进程子模块信息等功能,留到后面再来慢慢实现.这一篇来讲述如何对主界面进行个性化的定制.Qt库提供的只是最基本的组件功能,使用这些组件开 ...

  9. Linux - Ubuntu下JDK配置

    系统版本: ubuntu 14.04 x64JDK版本: jdk-8u60-linux-x64 1.查看系统位数,输入以下命令即可 getconf LONG_BIT 2.下载对应的JDK文件,我这里下 ...

  10. 在C#后端处理一些结果然传给前端Javascript或是jQuery

    在C#后端处理一些结果然传给前端Javascript或是jQuery,以前Insus.NET有做过一个例子<把CS值传给JS使用 >http://www.cnblogs.com/insus ...