多选按钮(CheckBox)
今天我们介绍的是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)的更多相关文章
- JQuery 多选按钮checkbox
JQuery 多选按钮checkbox 在需要全选和选择部分的时候我们就需要多选在这里主要介绍了具体的实现 JQuery $(function () { //全选或全不选 $("#allbo ...
- 多选按钮CheckBox
main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmln ...
- cocos2d-x JS 复选按钮checkBox的单选与多选
var HZ_createRoom = jx.BaseView.extend({//红中麻将 ctor : function() { this._super(); this.addLayout(res ...
- zepto全选按钮之全选会根据按钮是否被全部选中更改状态
在做手机端二次开发购物车的时候,发现zepto全选,没找到,或者功能不是自己想要的 后来做好,分享给需要的人 //全选或多选处理 var CheckAll = $('#items_check ...
- Android开发 ---基本UI组件2:图像按钮、单选按钮监听、多选按钮监听、开关
Android开发 ---基本UI组件2 1.activity_main.xml 描述: 定义一个按钮 <?xml version="1.0" encoding=" ...
- 『心善渊』Selenium3.0基础 — 14、Selenium对单选和多选按钮的操作
目录 1.页面中的单选按钮和多选按钮 2.判断按钮是否选中is_selected() 3.单选按钮的操作 4.多选按钮的操作 5.选择部分多选按钮的操作 1.页面中的单选按钮和多选按钮 页面中的单选按 ...
- checkbox做全选按钮
1.先写一个html页面,里面写一个全选按钮和几个复选框,实现下面2个要求 (1)点击全选按钮选中时,所有的复选框选中. (2)点击全选按钮取消选中时,所有复选框取消选中. <input typ ...
- js做全选,用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false
用一个checkbox复选框做多个checkbox复选框的全选按钮,有一个复选框未被选择时,全选按钮的checked就为false,当所有checkbox都被选中时,全选按钮也被选中. 详解: 有两种 ...
- 安卓开发_复选按钮控件(CheckBox)的简单使用
复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈 复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性 一.由于 ...
- 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义
参考文章: 微信小程序 修改(自定义) 单选/复选按钮样式 checkbox/radio样式自定义
随机推荐
- [Node.js] ECMAScript 6中的生成器及koa小析
原文地址:http://www.moye.me/2014/11/10/ecmascript-6-generator/ 引子 老听人说 koa大法好,这两天我也赶了把时髦:用 n 安上了node 0.1 ...
- 参数嗅探(Parameter Sniffing)(1/2)
这个问题会在参数话的SQL语句(例如存储过程)与SQL Server里的计划缓存机制结合的时候会出现.这个文章分为2个部分,第1部分会介绍下参数嗅探(Parameter Sniffing)的概况,第2 ...
- .NET中Main函数使用小技巧
摘要:任何语言开发出来的程序,都会有一个程序入口函数,可能每个语言所使用的程序入口函数名称不一样,但是它们的作用都是一样的,都是被操作系统去调用.那么本文主要总结.NET中的程序入口函数Main使用的 ...
- Mysql的“Limit”操作
Limit操作: ,; #返回第6-15行数据 ; #返回前5行 ,; #返回前5行 性能优化: 基于MySQL5.0中limit的高性能,我对数据分页也重新有了新的认识.测试SQL语句1: Sele ...
- SqL数据库发布订阅非聚集索引没有被复制到订阅服务器的解决方案
Non-Clustered Indexes not copying in Transactional Replication : SQL Server 2008 方法1: You have trans ...
- 记录一次Mac虚拟机安装的过程(有图有真相)
这是我今天在公司用Vmware workstation虚拟机安装小狮子的全过程,记录一下没什么特别的用途,希望以后不要忘记,整个过程我总共花了半个多小时,挺快的.确实苹果的系统配上苹果的电脑就是牛叉, ...
- csharp:asp.net Importing or Exporting Data from Worksheets using aspose cell
using System; using System.Data; using System.Configuration; using System.Collections; using System. ...
- ViewPager的使用
在上培训课的时候,老师一直在将ViewPager是现在的主流,一直想去好好的了解一下,今天去网上学习了一下 ,做一个总结: ViewPager其实就是后来谷歌提供给我们的一个组件,就像TextVi ...
- [python学习笔记]Day3
函数 如: def is_leapyear(year): if (year%4 == 0 and year%100 != 0) or (year%400 == 0): return True else ...
- 解决Spring MVC @ResponseBody返回html中中文字符串乱码问题
最近有个应用,通过responsebody返回完整的html页面时出现乱码是异常的问题,因为是通过responsebody返回,所以一开始设置了text/plain的字符集,如下: <mvc:a ...