简述RadioGroup和RadioButton的使用
简述RadioGroup和RadioButton的使用
在项目中有身份选择的选项,需要用到RadioGroup和RadioButton,这里做个记录,和大家交流一下。
话不多说,一起看代码
XML代码
<RadioGroup
android:id="@+id/login_radiogroup"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"> <RadioButton
android:id="@+id/admin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/admin"/> <RadioButton
android:id="@+id/tech"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/tech"/> <RadioButton
android:id="@+id/market"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/market"/> <RadioButton
android:id="@+id/guest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:text="@string/guest"/> </RadioGroup>
这里使用了layout_gravity让RadioGroup居中显示,使用了orientation属性,horizontal让内部的RadioButton呈水平摆放,vertical就是垂直摆放。
JAVA代码
这里因为在Fragment里面实例化布局,所有用了view.findViewById(),在Activity中的话去掉view就可以。
private RadioGroup mRadioGroup;
mRadioGroup=(RadioGroup)view.findViewById(R.id.login_radiogroup);
接下来在活动中实现对RadioGroup的监听。
mRadioGroup.setOnCheckedChangeListener(new CheckListener());
class CheckListener implements RadioGroup.OnCheckedChangeListener{
@Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId){
case R.id.admin:
//执行具体操作
break;
case R.id.tech:
//执行具体操作
break;
case R.id.market:
//执行具体操作
break;
case R.id.guest:
//执行具体操作
break;
default:
break;
}
}
}
是个菜鸟,有错误还希望大家能指出来。
欢迎大家有好的想法一起交流。
简述RadioGroup和RadioButton的使用的更多相关文章
- Android入门(八):使用RadioGroup 和RadioButton组件建立单选清单
这一章,我们学习RadioGroup 和RadioButton组件,我们新建一个项目,编码过程与前几章的项目类似. 1.建立字符串资源文件strings.xml: <resources> ...
- Android控件之RadioGroup与RadioButton(单选控件)
一.RadioGroup与RadioButton 1.什么是RadioGroup: RadioButton的一个集合,提供多选机制 2.什么是RadioButton: RadioButton包裹在Ra ...
- 使用RadioGroup与RadioButton实现多选一
RadioGroup是RadioButton的集合, RadioGroup里面可以包含很多RadioButton,提供多选一机制,只能选择其中一个 RadioGroup的orientation(方向) ...
- React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton)
React Native之(支持iOS与Android)自定义单选按钮(RadioGroup,RadioButton) 一,需求与简单介绍 在开发项目时发现RN没有给提供RadioButton和Rad ...
- 动态布局中RadioGroup的RadioButton有时候不相互排斥的原因
近期在做一个答题类的模块,有单选.简答.调查问卷等,我是用动态布局的方式生成答题项的.在弄单选的时候遇到一个比較奇葩的问题,在代码中生成RadioGroup和RadioButton的时候.会发现不能相 ...
- Android 使用RadioGroup和RadioButton实现单选效果
RadioButton和CheckBox的区别:CheckBox选中之后可以直接取消,RadioButton选中之后不能直接取消,所以一般情况下不建议单独使用.1.RadioGroup:RadioBu ...
- Android下利用RadioGroup和RadioButton实现Tabbar的效果
本实现方法主要使用RadioGroup和RadioButton的组合方式来实现Tabbar的效果. 其中选中的Tab的切换的动作可以通过RadioGroup的OnCheckedChangeListen ...
- 如何使用RadioGroup和RadioButton实现FragmentTabHost导航效果?
目录: 一.概述 最近在做一个新闻类结合社区的APP的时候,需要添加一个侧滑菜单的效果,考虑到可以使用DrawerLayout布局,但是问题是使用了 DrawerLayout布局后,主页内容应该是一个 ...
- Android RadioGroup和RadioButton详解
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGrou ...
随机推荐
- linux下编译安装python
从官网下载指定的源码包 https://www.python.org/downloads/source/ 把源码文件以二进制方式上传到linux服务器 安装python需要用到gcc工具,首先查看gc ...
- Android OpenGL 播放视频学习
1, 初步接触Open GL: http://www.cnblogs.com/TerryBlog/archive/2010/07/09/1774475.html 使用GLSurfaceView和Ren ...
- <ReversingEngineering>关于windows32位系统下的dll注入技术经验汇
上个学期把自己闷在图书馆一直在看关于逆向工程技术方面的书,从入门到初级,现在也敢说自己一条腿已经迈进了这片知识的大门里,因为该博客刚开通先将一些经验记录下来,也是留给自己一方面做个参照. <逆向 ...
- SDUT 2133 数据结构实验之栈三:后缀式求值
数据结构实验之栈三:后缀式求值 Time Limit: 1000ms Memory limit: 65536K 有疑问?点这里^_^ 题目描述 对于一个基于二元运算符的后缀表示式(基本操作数都是 ...
- HDU3247 Resource Archiver —— AC自动机 + BFS最短路 + 状压DP
题目链接:https://vjudge.net/problem/HDU-3247 Resource Archiver Time Limit: 20000/10000 MS (Java/Others) ...
- win 10 安装.msi 程序出现the error code is 2503
解决方法: C:\Windows\temp文件夹的权限不够,需要给其更高权限 右键temp文件夹 点击属性进入属性对话框 组或用户名的里面的All APPLICATION PACKAGES和所有受限制 ...
- python类初探
class human: is_alive=True is_man=True def __init__(self,age): print('this is __init__() method, whi ...
- Linux_服务器_05_CentOS 7安装完成后初始化的方法_Linux
参考资料 1.CentOS 7安装完成后初始化的方法_Linux
- Android Studio的技巧
1.快速添加add unimplements methods: 右键generate 2.快速添加try-catch:左边就有一个小电灯,然后可以选. 3.格式化OPTION + CMD + L ( ...
- star score
<!DOCTYPE html> <html> <head> <meta charset="utf-8"> <meta http ...