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 ...
随机推荐
- phpexcel导出乱码
在输出到浏览器时,要在head前加ob_end_clean();参考此文, ob_end_clean(); header('Content-Type: application/vnd.ms-excel ...
- Java 并发编程学习笔记 理解CLH队列锁算法
CLH算法实现 CLH队列中的结点QNode中含有一个locked字段,该字段若为true表示该线程需要获取锁,且不释放锁,为false表示线程释放了锁.结点之间是通过隐形的链表相连,之所以叫隐形的链 ...
- Java命令学习系列(三)——Jmap
Java命令学习系列(三)——Jmap 2015-05-16 分类:Java 阅读(479) 评论(0) Jmap jmap是JDK自带的工具软件,主要用于打印指定Java进程(或核心文件.远程调试服 ...
- 我的mac的其他满了,发现是一个叫core的文件
6Q12KFX%PDARS7B{B__OZVW.jpg (97.93 KB, 下载次数: 0) 下载附件 保存到相册 2014-7-3 15:40 上传 如图,电脑虽然买了半年但是基本没怎么用, ...
- javascript 鼠标方式去显示
document.write("<style type='text/css'>"); document.write(".yyfloat_p{width:209 ...
- [转]getHibernateTemplate出现的所有find方法的总结
一.find(String queryString); 示例:this.getHibernateTemplate().find("from bean.User"); 返回所有Use ...
- hadoop 参数
看<Hadoop:权威指南>的时候收集了书上写的一些需要优化的参数,记录了一下子,给大家分享一下吧. 1.mapred.task.timeout 任务超时时间,默认是10分钟 2.mapr ...
- linux rename 和?????
[root@ob2 mytmp]# ls01.txt 02.txt 03.txt aa2.txt aa.sh aa.txt tp_2017-09-143516.tar.gz tt [ro ...
- C++ IO流小结
撒花庆祝下,终于看完了(C++Primer)第一部分,即将进入第二部分! IO部分,最基本的是iostream(istream.ostream),子类有fstream(ifstream.ofstrea ...
- yuv420格式分析
http://blog.csdn.net/liuhongxiangm/article/details/9135791 http://blog.csdn.net/bluesky_sunshine/art ...