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

复选按钮用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. System.Collections.Generic 源码阅读总结

    ArrayList ,List ArrayList 和 List 都是不限制长度的集合类型 ,List相比ArrayList 就内部实现而言除了泛型本质没有太大区别.不过为避免装箱拆箱问题,尽可能使用 ...

  2. LeetCode--No.006 ZigZag Conversion

    6. ZigZag Conversion Total Accepted: 98584 Total Submissions: 398018 Difficulty: Easy The string &qu ...

  3. null、undefined、typeof、instanceof

    目录 概述 null undefined typeof instanceof 概述 JavaScript(ECMAScript标准)里共有5种基本类型: undefined, null, Boolea ...

  4. 深度解析 Vue 响应式原理

    深度解析 Vue 响应式原理 该文章内容节选自团队的开源项目 InterviewMap.项目目前内容包含了 JS.网络.浏览器相关.性能优化.安全.框架.Git.数据结构.算法等内容,无论是基础还是进 ...

  5. 求一个集合的所有真子集 Python

    给定一个集合,元素均为正整数且不重复,求该集合的所有子集 # -*- coding: utf-8 -*- """ Created on Tue Oct 10 09:04: ...

  6. Postgres绿色版本安装 windows

    虽然PostgreSQL是为类UNIX平台开发的,但它却是可以移植的.从7.1版本开始,PostgreSQL可以编译安装和作为一个PostgreSQL服务器运行在Windows NT 4,2000,X ...

  7. 完整的一次 HTTP 请求响应过程(二)

    上篇文章 我们完整的描述了计算机五层模型中的『应用层』和『运输层』,阐述了较为复杂的 TCP 协议的相关原理,相信大家一定也有所收获,那么本篇将继续五层模型的学习. 网络层 『网络层』其实解决的就是一 ...

  8. USB 驱动(监测鼠标左键的动作)

    (基于 Linux 3.4.2 内核) 可分为以下几个步骤来完成这个驱动: 1. 分配设置一个 usb_driver 结构体 2. 注册这个 usb_driver (如果设备的 id_table 与驱 ...

  9. JavaWeb总结(国税)

    一.JavaWeb示例 1.1.Servlet Hello World&IDEA创建第一个Servlet 新建项目 选择maven 组织名与项目名 位置,完成 允许自动导入包 添加框架支持,变 ...

  10. osgi.net框架简介

    osgi.net是一个动态的模块化框架.它向用户提供了模块化与插件化.面向服务构架和模块扩展支持等功能.该平台是OSGi联盟定义的服务平台规范移植到.NET的实现. 简介 尤埃开放服务平台是一个基于. ...