单选按钮(RadioButton)与复选框(CheckBox)的功能与用法
单选按钮(RadioButton)和复选框(CheckBox)、状态开关按钮(ToggleButton)与开关(Switch)是用户界面中最普通的UI组件,他们都继承了Button类,因此都可直接使用Button支持的各种属性和方法。
RadioButton、CheckBo与普通按钮不同的是,它们多了一个可选中的功能,因此RadioButon、CheckBox都可额外指定一个android:checked属性,该属性用于指定RadioButton、CheckBox初始时是否被选中。
RadioButton与CheckBox的不同之处在于,一组RadioButton只能选中其中一个,因此RadioButton通常要与RadioGroup一起使用,用于定义一组单选按钮。
实例:利用单选按钮、复选框按钮获取用户信息
在需要获取用户信息的界面中,有些信息不需要用户直接输入,可以考虑让用户进行选择,比如用户的性别、爱好等。下面的界面布局文件定义一个让用户选择的输入界面。
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性别:"
android:textSize="16dp"/>
<!-- 定义一组单选按钮 -->
<RadioGroup android:id="@+id/rg"
android:orientation="horizontal"
android:layout_gravity="center_horizontal">
<!-- 定义两个单选按钮 -->
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/male"
android:text="男"
android:checked="true" />
<RadioButton android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/female"
android:text="女"/>
</RadioGroup>
</TableRow>
<TableRow >
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="喜欢的颜色:"
android:textSize="16dp"/>
<!-- 定义一个垂直的线性布局 -->
<LinearLayout
android:layout_gravity="center_horizontal"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<!-- 定义三个复选框 -->
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="红色"
android:checked="true" />
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="蓝色"/>
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="绿色"/>
</LinearLayout>
</TableRow>
<TextView android:id="@+id/show"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</TableLayout>
上面的界面布局中定义了一组单选按钮,并默认勾选了第一个单选按钮,这组单选按钮供用户选择性别:还定义了三个复选框,供用户选择喜欢的颜色。
注意:如果在XML布局文件中默认勾选了某个单选按钮,则必须为该组单选按钮的每个按钮指定android:id属性值,否则这组单选按钮不能正常工作。
为了监听单选按钮、复选框勾选状态的改变,可以为它们添加事件监听器。例如下面Activity为RadioGroup添加了事件监听器,该监听器可以监听这组单选按钮的勾选状态的改变。
package org.crazyit.helloworld; import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.widget.*;
import android.widget.RadioGroup.*; public class RaidoButtonCheckBoxTest extends Activity {
RadioGroup rg;
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.raido_button_check_box_test);
//获取界面上rg、show两个组件
rg=(RadioGroup)findViewById(R.id.rg);
show=(TextView)findViewById(R.id.show);
//为RadioGroup组件的OnCheck事件绑定事件监听器
rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){ @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
//根据用户勾选的单选按钮来动态改变tip字符串的值
String tip=checkedId==R.id.male?"您的性别是男人":"您的性别是女人";
//修改show组件中的文本
show.setText(tip);
} });
} @Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.raido_button_check_box_test, menu);
return true;
} }
运行上面的程序,并改变第一组单选按钮的勾选状态,将看到如图2.25所示界面
图2.25 单选按钮、复选框示意图
单选按钮(RadioButton)与复选框(CheckBox)的功能与用法的更多相关文章
- Android 单选按钮(RadioButton)和复选框(CheckBox)的使用
1.RadioButton (1)介绍 (2)单选按钮点击事件的用法 (3)RadioButton与RadioGroup配合使用实现单选题功能 (4)xml布局及使用 <?xml version ...
- 3.Android之单选按钮RadioGroup和复选框Checkbox学习
单选按钮和复选框在实际中经常看到,今天就简单梳理下. 首先,我们在工具中拖进单选按钮RadioGroup和复选框Checkbox,如图: xml对应的源码: <?xml version=&quo ...
- 安卓开发:UI组件-RadioButton和复选框CheckBox
2.5RadioButton 为用户提供由两个及以上互斥的选项组成的选项集. 2.5.1精简代码 在按钮变多之后,多次重复书写点击事件有些繁琐,我们在这里创建一个事件OnClick,每次点击时调用该事 ...
- 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)
原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1, ...
- 关于bootstrap--表单(下拉<select>、输入框<input>、文本域<textare>复选框<checkbox>和单选按钮<radio>)
html 里面的 role 本质上是增强语义性,当现有的HTML标签不能充分表达语义性的时候,就可以借助role来说明.通常这种情况出现在一些自定义的组件上,这样可增强组件的可访问性.可用性和可交互性 ...
- [原创]纯JS实现网页中多选复选框checkbox和单选radio的美化效果
图片素材: 最终效果图: <html><title> 纯JS实现网页中多选复选框checkbox和单选radio的美化效果</title><head>& ...
- css3美化复选框checkbox
两种美化效果如下图: 代码(html) <div id="main"> <h2 class="top_title">使用CSS3美化复 ...
- 复选框(checkbox)、单选框(radiobox)的使用
复选框(checkbox).单选框(radiobox)的使用 复选框: HTML: // 复选框 <input type="checkbox" name="chec ...
- php 判断复选框checkbox是否被选中
php 判断复选框checkbox是否被选中 复选框checkbox在php表单提交中经常被使用到,本文章通过实例向大家介绍php如何判断复选框checkbox中的值是否被选中,需要的朋友可以参考 ...
- jquery判断复选框checkbox是否被选中
jquery判断复选框checkbox是否被选中 使用is方法 //如果选中返回true //如果未选中返回false .is(':checked');
随机推荐
- hihoCoder 1252 Kejin Game
2015 ACM / ICPC 北京站 D题 网络最大流 和同学讨论了一会儿,还是Xiang578机智... ... /* ************************************** ...
- 关于JSON.parse在ie6,ie7下未定义的issue
情况是这样的: 在ie6下出现一个js error,说是JSON.parse为定义,一查,才知道,ie6,ie7不支持JSON. solution:只要在使用JSON之前加载个json2.js就行了. ...
- bootstrap-table 表头和内容对不齐解决办法
偶然机会学习bootstrap,表格利用bootstrap-table实现,使用bootstrap-table过程中,发现了一个非常棘手的问题,在ie浏览器中,表格的表头和内容对不齐,特别是列比较多且 ...
- 杀死进程kill和fuser
1 kill -9 id 2 不行的话使用 fuser -k -SIGHUP /opt/bre/cookiemapping/wsapi/123 fuser有一个特别的用法在于它可以一次杀死那 ...
- mysql安装使用--2 用户管理
1 修改mysql.user表 添加用户 mysql> INSERT INTO mysql.user (Host,User,Password) VALUES (\'%\',\'system\', ...
- 安卓图表引擎AChartEngine(五) - Dataset和Render参数介绍
下面只讲解一个Renderer和一个DataSet,更多内容参看官方文档 DefaultRenderer: void addSeriesRenderer(int index, SimpleSeries ...
- android里uri和url的区别
URI :是从虚拟根路径开始的 URI,是uniform resource identifier URL:是整个链接 URI,是uniform resource location uri:file: ...
- 最简单的ajax调用webservice
<%@ Page Language="C#" AutoEventWireup="true" CodeBehind="TestHelloWorld ...
- Apache处理请求步骤及过程
Apache请求处理循环详解 : 1.Post-Read-Request阶段: 在正常请求处理流程中,这是模块可以插入钩子的第一个阶段.对于那些想很早进入处理请求的模块来说,这个阶段可以被利用. 2. ...
- windows 下nginx 虚拟主机搭建
需要在 nginx.conf里面引入刚才配置的那个文件 第一步 加东西 http的节点里面加上 一定要注意的是:必须以 ; 结尾 include D:/phpen/nginx-1.3.6/co ...