Android 使用RadioGroup和RadioButton实现单选效果
RadioButton和CheckBox的区别:CheckBox选中之后可以直接取消,RadioButton选中之后不能直接取消,所以一般情况下不建议单独使用。
1.RadioGroup:
RadioButton的一个集合,提供多选一机制。
2.属性:
android:orientation ("vertical"--垂直排布;"horizontal"--水平排布)
决定当前RadioGroup中RadioButton以什么形式排列
使用RadioButton和RadioGroup实现多选一效果
监听可以使用RadioGroup的OnClickeListener,也可以使用RadioButton的OnCliekedChangeListener。
样例程序包含一个包含两个RadioButton的RadioGroup和一个TextView,TextView会实时显示选中的性别信息。
package com.example.radiogroupradiobutton; import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView; public class MainActivity extends ActionBarActivity { private RadioGroup radioGroup;
private TextView textView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); textView = (TextView) findViewById(R.id.textView1); radioGroup = (RadioGroup) findViewById(R.id.radioGroup1);
radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radio0: textView.setText("选中:男"); break;
case R.id.radio1: textView.setText("选中:女"); break;
default: textView.setText("未选中"); break;
}
} });
} }
MainActivity.java
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
> <RadioGroup
android:orientation="vertical"
android1:id="@+id/radioGroup1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content" > <RadioButton
android1:id="@+id/radio0"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:checked="true"
android1:text="男" /> <RadioButton
android1:id="@+id/radio1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:text="女" /> </RadioGroup> <TextView
android1:id="@+id/textView1"
android1:layout_width="wrap_content"
android1:layout_height="wrap_content"
android1:text="选中:男" /> </LinearLayout>
activity_mail.xml
效果:

Android 使用RadioGroup和RadioButton实现单选效果的更多相关文章
- Android控件之RadioGroup与RadioButton(单选控件)
一.RadioGroup与RadioButton 1.什么是RadioGroup: RadioButton的一个集合,提供多选机制 2.什么是RadioButton: RadioButton包裹在Ra ...
- Android下利用RadioGroup和RadioButton实现Tabbar的效果
本实现方法主要使用RadioGroup和RadioButton的组合方式来实现Tabbar的效果. 其中选中的Tab的切换的动作可以通过RadioGroup的OnCheckedChangeListen ...
- Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单
这一章,我们学习RadioGroup 和RadioButton组件,我们新建一个项目,编码过程与前几章的项目类似. 1.建立字符串资源文件strings.xml: <resources> ...
- Android初级教程小案例之单选框RadioGroup与复选框CheckBox
Android里面的单选框和html中的其实是一样的效果.这里用到两个控件:CheckBox和RadioGroup.直接上代码: radio.xml布局文件: <?xml version=&qu ...
- Android学习之RadioGroup和RadioButton
转载自:http://my.oschina.net/amigos/blog/59261 实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioG ...
- RadioGroup和RadioButton(单选框)
1.布局文件 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:t ...
- Android RadioGroup和RadioButton详解
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...
- 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...
- React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)
React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...
随机推荐
- 纯css实现进度条效果
去年7月份做一个公司商城的微信页面(微信用的chrome内核)需要写一个提示返现进度的进度条效果. 一个完整的进度条效果其实可以拆分一下: 一段背景: 一小段的静态的斜纹进度条: 斜纹进度条用线性渐变 ...
- C语言 · 明明的随机数
算法训练 明明的随机数 时间限制:1.0s 内存限制:256.0MB 问题描述 明明想在学校中请一些同学一起做一项问卷调查,为了实验的客观性,他先用计算机生成了N个1到1000之间的 ...
- sql server 查找主键、外键、索引、约束
主键约束 SELECT tab.name AS [表名], idx.name AS [主键名称], col.name AS [主键列名] FROM sys.indexes idx ...
- [Linux应用]Linux应用程序输出数据重定向到文件中
转自:http://blog.chinaunix.net/uid-20680966-id-4698387.html 目的是要让程序的printf的打印能重定向到某个文本中,ctrl+c强制退出后查看文 ...
- HashMap与ConcurrentHashMap的区别<转>
从JDK1.2起,就有了HashMap,正如前一篇文章所说,HashMap不是线程安全的,因此多线程操作时需要格外小心. 在JDK1.5中,伟大的Doug Lea给我们带来了concurrent包,从 ...
- java定时任务Timer与ScheduledExecutorService<转>
在我们编程过程中如果需要执行一些简单的定时任务,无须做复杂的控制,我们可以考虑使用JDK中的Timer定时任务来实现.下面LZ就其原理.实例以及Timer缺陷三个方面来解析java Timer定时器. ...
- Entity Framework管理实体关系(一):管理一对一关系
我们现在已经知道如何使用Code First来定义简单的领域类,并且如何使用DbContext类来执行数据库操作.现在我们来看下数据库理论中的多样性关系,我们会使用Code First来实现下面的几种 ...
- nodejs 模块以及加载机制,主要讨论找不到模块的问题
最主要的一个思想,加载模块无非就是找到模块在哪,只要清楚了模块的位置以及模块加载的逻辑那么找不到模块的问题就迎刃而解了.本文只是综合了自己所学的知识点进行总结,难免出现理解错误的地方,请见谅. nod ...
- loadrunner循环执行某个动作
1.action部分定义 int i; int count; 2. 打算循环的代码前代码如下: count=rand() % 8 +1; for(i=0;i<coun ...
- 【转】【Mysql】MySQL添加用户、删除用户与授权
MySql中添加用户,新建数据库,用户授权,删除用户,修改密码(注意每行后边都跟个;表示一个命令语句结束): 1.新建用户 1.1 登录MYSQL: @>mysql -u root -p @&g ...