Android—RadioButton(单选框)和CheckBox(复选框)
一、RadioButton和CheckBox的区别:
1、单个RadioButton在选中后,通过点击无法变为未选中
单个CheckBox在选中后,通过点击可以变为未选中
2、一组RadioButton,只能同时选中一个
一组CheckBox,能同时选中多个
3、RadioButton在大部分UI框架中默认都以圆形表示
CheckBox在大部分UI框架中默认都以矩形表示
二、RadioButton和RadioGroup的关系:
1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器
2、每个RadioGroup中的RadioButton同时只能有一个被选中
3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中
4、大部分场合下,一个RadioGroup中至少有2个RadioButton
5、RadioGroup和CheckBox控件设置监听器都是用的setOnCheckedChangeListener函数,其输入参数是一个函数,且函数内部要实现1个内部类。RadioGroup监听器的输入参数用的是RadioGroup.OnCheckedChangeListener(),而CheckBox监听器的输入参数用的是函数CompoundButton.OnCheckedChangeListener().
代码:
MainActivity:
package com.turo.checkboxtest;import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast;public class MainActivity extends AppCompatActivity implements CompoundButton.OnCheckedChangeListener{//定义各控件的变量private TextView who = null;private TextView how = null;private RadioGroup who_group = null;private RadioButton china = null;private RadioButton america = null;private RadioButton others = null;private CheckBox cinema = null;private CheckBox computer = null;private CheckBox DVD = null;private CheckBox phone = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);//获得对应的控件who = (TextView)findViewById(R.id.who);how = (TextView)findViewById(R.id.how);who_group = (RadioGroup)findViewById(R.id.who_group);china = (RadioButton)findViewById(R.id.china);america = (RadioButton)findViewById(R.id.america);others = (RadioButton)findViewById(R.id.others);cinema = (CheckBox)findViewById(R.id.cinema_bc);computer = (CheckBox)findViewById(R.id.computer_bc);DVD = (CheckBox)findViewById(R.id.DVD_bc);phone = (CheckBox)findViewById(R.id.phone_bc);cinema.setOnCheckedChangeListener(this);computer.setOnCheckedChangeListener(this);DVD.setOnCheckedChangeListener(this);phone.setOnCheckedChangeListener(this);//设置who_group的监听器,其实是一句代码,其参数是一个带有重构函数的对象who_group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubif(checkedId == china.getId()){Toast.makeText(MainActivity.this,"中国", Toast.LENGTH_SHORT).show();}else if(checkedId == america.getId()){Toast.makeText(MainActivity.this, "美国", Toast.LENGTH_SHORT).show();}else if(checkedId == others.getId()){Toast.makeText(MainActivity.this, "其它国家", Toast.LENGTH_SHORT).show();}}});}@Overridepublic void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {if(cinema.isChecked()){Toast.makeText(MainActivity.this,"电影院观看",Toast.LENGTH_SHORT).show();}if(phone.isChecked()){Toast.makeText(MainActivity.this,"手机观看",Toast.LENGTH_SHORT).show();}if(DVD.isChecked()){Toast.makeText(MainActivity.this,"DVD观看",Toast.LENGTH_SHORT).show();}if(computer.isChecked()){Toast.makeText(MainActivity.this,"电脑观看",Toast.LENGTH_SHORT).show();}}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical"><TextView
android:id="@+id/who"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/who"/><RadioGroup
android:id="@+id/who_group"android:layout_width="wrap_content"android:layout_height="wrap_content"android:orientation="vertical"><RadioButton
android:id="@+id/china"android:layout_height="wrap_content"android:layout_width="wrap_content"android:text="@string/china"/><RadioButton
android:id="@+id/america"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/america"/><RadioButton
android:id="@+id/others"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/others"/></RadioGroup><TextView
android:id="@+id/how"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/how"/><CheckBox
android:id="@+id/cinema_bc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/cinema"/><CheckBox
android:id="@+id/computer_bc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/computer"/><CheckBox
android:id="@+id/DVD_bc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/DVD"/><CheckBox
android:id="@+id/phone_bc"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/phone"/>
</LinearLayout>
效果图:
上面3个为一组RadioGroup,每选中其中一个RadioButton,则会有相应的提示。且只能选中其中的一个。
下面的4都为CheckBox,可以选中其中的多个。每个CheckBox被选中都有相应的文字提示小窗口。
Android—RadioButton(单选框)和CheckBox(复选框)相关推荐
- android 多个复选框,Android UI控件之CheckBox(复选框、多选框)
上一篇文章中学习了RadioButton.知道了RadionButton的基本用法,实现起来也是听简单的 和RadioButton一样CheckBox也是一个使用的非常多的控件,Android中它的的 ...
- Android基础入门教程——2.3.5.RadioButton(单选按钮)Checkbox(复选框)
Android基础入门教程--2.3.5.RadioButton(单选按钮)&Checkbox(复选框) 标签(空格分隔): Android基础入门教程 本节引言: 本节给大家带来的是Ando ...
- js checkbox复选框实现单选功能
本文仅供学习交流使用,demo下载见文末 js checkbox复选框实现单选功能 <script type="text/javascript">$(":ch ...
- android勾选控件_Android中CheckBox复选框控件使用方法详解
CheckBox复选框控件使用方法,具体内容如下 一.简介 1. 2.类结构图 二.CheckBox复选框控件使用方法 这里是使用java代码在LinearLayout里面添加控件 1.新建Linea ...
- 如何让checkbox复选框只能单选
如何让checkbox复选框只能单选 在项目开发中遇到一个这样的问题,要让一列复选框架在任何时间段内只能选择一个. 有人说怎么不用单选框了,因为单选框一旦选择了就不能取消选择,所以这里只能用复选框. ...
- Ext.form.field.CheckBox复选框和Ext.form.field.Radio单选框
1.Ext.form.field.CheckBox和Ext.form.field.Radio主要配置项目 配置项 类型 说明 boxLabel String 紧靠复选框的文字描述 boxLabelAl ...
- 实战总结(二)—— CheckBox复选框和SpannableString
CheckBox复选框和SpannableString 项目评估说登录时默认选择已阅读相关协议,应有复选框,于是做出以下改动:默认不勾选checkBox,在不勾选checkBox情况下无法登,会弹出T ...
- checkbox(复选框)和radio(单选按钮)的区别与详解
选中和取消 选中一个 checkbox(复选框) 后,再次点击它,即可取消选中 选中一个 radio(单选按钮) 后,再次点击它,不能取消选中 <!DOCTYPE html> <ht ...
- checkbox复选框样式
随着现代浏览器的流行,纯CSS设置checkbox也变的很是实用,下面会讲到5种与众不同的checkbox复选框. 首先,需要添加一段CSS隐藏所有的Checkbox复选框,下面我们会改变它的外观.要 ...
- js获取checkbox复选框获取选中的选项
分享下javascript获取checkbox 复选框获取选中的选项的方法. 有关javascript 获取checkbox复选框的实例数不胜数. js实现: var form = document. ...
最新文章
- s10 linpus linux,上月国外最受关注NB排行 超便携本占优
- java calendar.add方法_Java Calendar add()方法与示例
- 【杂谈】我在有三AI从学生到老师
- 在linux中查看端口信息,怎样在 Linux 上查看某个端口的相关信息?
- 【Source Insight 4.0 英文版】 如何设置选中高亮
- python--Time(时间)模块
- oracle什么是swap分区,linux 安装oracle时swap空间不足
- java持久性与安全性_Java持久性锁定初学者指南
- ServletRequest startAsync()的有用性有限
- mysql 磁盘限额_Linux运维知识之为Linux MySQL数据库设置磁盘限额
- Oracle的回收站和闪回查询机制(二)
- 设置hash后导致的返回问题的解决方案
- Race Condition是什么
- 《麦肯锡方法》第1章建立解决方案-思维导图
- IIS无法启动:存储空间不足解决办法
- 什么是主数据?有什么作用?
- WIN7 通过4G模块 拨号上网过程
- 毕业设计之 --- 基于设深度学习的人脸性别年龄识别系统
- tooth的用法_tooth的复数和用法例句
- Axure 交互设计 (1)