复选按钮 即可以选择若干个选项,与单选按钮不同的是,复选按钮的图标是方块,单选按钮是圆圈

复选按钮用CheckBox表示,CheckBox是Button的子类,支持使用Button的所有属性

一、由于复选框可以选中多项,所有为了确定用户是否选择了某一项,还需要为每一个选项添加setOnCheckedChangeListener事件监听

例如:

为id为like1的复选按钮添加状态改变事件监听,代码如下

 final CheckBox like1 = (CheckBox)findViewById(R.id.like1);
//监听事件 like1.setOnCheckedChangeListener(new OnCheckedChangeListener()){ @Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub
if(like1.isChecked())
like1.getText();
}
});

二、使用示例

先看布局文件

<?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" > <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="选择您的爱好"
android:textSize="19dp"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_checkbox_1"
android:text="音乐"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_checkbox_2"
android:text="美术"
/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/id_checkbox_3"
android:text="体育"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="提交"
android:id="@+id/btn_checkbox_tijiao"
/>
</LinearLayout>

效果图:

再看JAVA文件

 package base_ui;

 import com.example.allcode.R;

 import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.Checkable;
import android.widget.CompoundButton;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.Toast; public class Ui_CheckBox extends Activity implements android.widget.CompoundButton.OnCheckedChangeListener{
private Button tijiao;
private CheckBox checkbox_1;
private CheckBox checkbox_2;
private CheckBox checkbox_3;
private OnCheckedChangeListener checkbox_listen ;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.base_ui_checkbox); tijiao = (Button) findViewById(R.id.btn_checkbox_tijiao); checkbox_1 = (CheckBox) findViewById(R.id.id_checkbox_1);
checkbox_2 = (CheckBox) findViewById(R.id.id_checkbox_2);
checkbox_3 = (CheckBox) findViewById(R.id.id_checkbox_3);
tijiao = (Button) findViewById(R.id.btn_checkbox_tijiao); checkbox_1.setOnCheckedChangeListener(this);
checkbox_2.setOnCheckedChangeListener(this);
checkbox_3.setOnCheckedChangeListener(this); tijiao.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
String str=""; //存放选中的选项的值
if(checkbox_1.isChecked())
str+=checkbox_1.getText().toString()+" ";
if(checkbox_2.isChecked())
str+=checkbox_2.getText().toString()+" ";
if(checkbox_3.isChecked())
str+=checkbox_3.getText().toString()+" ";
Toast.makeText(Ui_CheckBox.this, "您选择的喜欢的爱好为:"+str, ).show(); }
});
}
//监听事件
@Override
public void onCheckedChanged(CompoundButton arg0, boolean arg1) {
// TODO Auto-generated method stub } }

可以看到,代码是很简单的,只有一个方法需要学习

checkbox_1.isChecked()

返回checkbox_1对应的复选按钮控件是否被选中

效果图:

												

安卓开发_复选按钮控件(CheckBox)的简单使用的更多相关文章

  1. 安卓开发_深入学习ViewPager控件

    一.概述 ViewPager是android扩展包v4包(android.support.v4.view.ViewPager)中的类,这个类可以让用户左右切换当前的view. ViewPager特点: ...

  2. css input checkbox复选框控件 样式美化的多种方案

    checkbox复选框可能是网站中常用的html元素,但大多数人并不满意它的默认样式,这篇文章就讲讲如何实现input checkbox复选框控件 样式美化效果. 资源网站大全 https://55w ...

  3. Java通过复选框控件数组实现添加多个复选框控件

    编写程序,通过复选框控件数组事先选择用户爱好信息的复选框,在该程序中,要求界面中的复选框数量可以根据指定复选框名称的字符串数组的长度来自动调节. 思路如下: 创建JPanel面板对象: 使用JPane ...

  4. CheckBox复选框控件

    CheckBox复选框控件 一.简介 1. 2.类结构图 二.CheckBox复选框控件使用方法 这里是使用java代码在LinearLayout里面添加控件 1.新建LinearLayout布局 2 ...

  5. [js开源组件开发]js多选日期控件

    js多选日期控件 详情请见:http://www.lovewebgames.com/jsmodule/calendar.html 它的github地址:https://github.com/tianx ...

  6. 【Android】11.0 UI开发(二)——列表控件ListView的简单实现1

    ************************ 转载请注明出处:https://www.cnblogs.com/xiaofu007/p/10342462.html ***************** ...

  7. Asp.net自定义单选复选框控件

    将常用的jquery插件封装成控件也是个不错的选择 下面是效果的简单颜色,由于博客系统的限制没法完整演示最终效果,请下载示例查看 Asp.netWeb APIC#Javascript   1.新建类库 ...

  8. 转 纯CSS设置Checkbox复选框控件的样式

    Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...

  9. 【转】纯CSS设置Checkbox复选框控件的样式

    Checkbox复选框是一个可能每一个网站都在使用的HTML元素,但大多数人并不给它们设置样式,所以在绝大多数网站它们看起来是一样的.为什么不把你的网站中的Checkbox设置一个与众不同的样式,甚至 ...

随机推荐

  1. 关于Android的Service知识点,你知道吗?

    目录 学习Service相关知识点: 概述: Service生命周期: Service的基本用法: 服务. 问:达叔,今日工作累吗? 答:累啊,那么问你,你知道Android中的 Service(服务 ...

  2. Linux 系统资源管理-top-cpu

  3. C语言中全局结构体指针隐含的错误

    前天在嵌入式系统上,调试一个数组的全局变量时,发现该变量一直会动态变化.深入分析, 才发现该全局结构体没有申请内存,而是用了一个指针.这种情况编译器是检查不出来的,在linux 上运行会挂掉,但是在裸 ...

  4. Python函数学习——匿名函数

    匿名函数 匿名函数就是不需要显式的指定函数名. 关键字lambda表示匿名函数,冒号前面的n表示函数参数,可以有多个参数. 匿名函数有个限制,就是只能有一个表达式,不用写return,返回值就是该表达 ...

  5. 全网最详细的Eclipse里如何正确新建普通的Java web项目并发布到Tomcat上运行成功【博主强烈推荐】(图文详解)

    不多说,直接上干货! 首先,大家要明确,IDEA.Eclipse和MyEclipse等编辑器之间的新建和运行手法是不一样的. 如果是在Myeclipse里,则是File -> new -> ...

  6. 从零开始学 Web 之 移动Web(二)JD移动端网页,移动触屏事件

    大家好,这里是「 从零开始学 Web 系列教程 」,并在下列地址同步更新...... github:https://github.com/Daotin/Web 微信公众号:Web前端之巅 博客园:ht ...

  7. Linux下网络文件系统NFS服务搭建易错点总结

    一.环境准备: 1 [root@czh ~]# cat /etc/redhat-release 2 CentOS release 6.7 (Final) 3 [root@czh ~]# uname - ...

  8. Kafka命令清单

    一.队列常用命令 #创建topics $ ./kafka-topics.sh --create --zookeeper chenx02:2181 --replication-factor 1 --pa ...

  9. 团队项目alpha冲刺

    Deadline: 2018-11-18 22:00PM,以博客提交至班级博客时间为准 提交: (a) 项目课堂演示: (b) 7篇冲刺随笔,组内可共享: (c) 1篇测试随笔,组内可共享: (d) ...

  10. .7-浅析webpack源码之WebpackOptionsDefaulter模块

    WebpackOptionsDefaulter模块 通过参数检测后,会根据单/多配置进行处理,本文基于单配置,所以会进行到如下代码: if (Array.isArray(options)) { com ...