RadioButton实现多选一
RadioButton实现多选一
一、简介

二、RadioButton实现多选一方法
1、将多个RadioButton放在一个RadioGroup里面
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textColor="#FFFFFF" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textColor="#FFFFFF" />
</RadioGroup>
2、在RadioGroup里面取出每个RadioButton
public void onClick(View v) {
// TODO Auto-generated method stub
int len = radioGroup1.getChildCount();
for (int i = 0; i < len; i++) {
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i); }
}
3、检查每个RadioButton是否被选取
if (radio.isChecked()) {
break;
}
4、取出被选取的那个RadioButton里面的值
Toast.makeText(Activity01.this, radio.getText(),
Toast.LENGTH_LONG).show();
三、代码实例
效果图:

代码:
fry.Activity01
package fry; import com.example.RadioButtonDemo1.R; import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.Toast; public class Activity01 extends Activity {
private Button btn_chooseGender;
private RadioGroup radioGroup1;
private TextView tv_answer; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01); btn_chooseGender = (Button) findViewById(R.id.btn_chooseGender);
radioGroup1 = (RadioGroup) findViewById(R.id.radioGroup1);
tv_answer = (TextView) findViewById(R.id.tv_answer);
/*
* RadioButton实现多选一方法
* 1、将多个RadioButton放在一个RadioGroup里面
* 2、在RadioGroup里面取出每个RadioButton
* 3、检查每个RadioButton是否被选取
* 4、取出被选取的那个RadioButton里面的值
*/
btn_chooseGender.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// TODO Auto-generated method stub
int len = radioGroup1.getChildCount();
for (int i = 0; i < len; i++) {
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
if (radio.isChecked()) {
Toast.makeText(Activity01.this, radio.getText(),
Toast.LENGTH_LONG).show();
break;
}
}
}
});
}
}
/RadioButtonDemo1/res/layout/activity01.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/black"
android:orientation="vertical" > <TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="性别"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF" /> <RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="match_parent"
android:layout_height="wrap_content" > <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="男"
android:textColor="#FFFFFF" /> <RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女"
android:textColor="#FFFFFF" />
</RadioGroup> <Button
android:id="@+id/btn_chooseGender"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="选择性别"
android:textColor="#FFFFFF" />
/> <TextView
android:id="@+id/tv_answer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_gravity="center_horizontal"
android:textColor="#FFFFFF" />
</LinearLayout>
四、收获
1、
android:textColor="#FFFFFF"
设置颜色,直接用#FFFFFF
2、
android:layout_gravity="center_horizontal"
文字居中显示
3、
RadioButton在RadioGroup里面实现多选一
4、
android:background="@android:color/black"
设置黑色,系统自带颜色
5、
int len = radioGroup1.getChildCount();
RadioGroup获取孩子数量
6、
RadioButton radio = (RadioButton) radioGroup1.getChildAt(i);
RadioGroup获取孩子
7、
if (radio.isChecked())
判断RadioButton是否被选取
8、
Toast.makeText(Activity01.this, radio.getText(),Toast.LENGTH_LONG).show();
吐司
RadioButton实现多选一的更多相关文章
- 可分组的选择框控件(MVVM下)(Toggle样式 仿造单选框RadioButton,复选框CheckBox功能)
原地址: http://www.cnblogs.com/yk250/p/5660340.html 效果图如下:支持分组的单选框,复选框样式和MVVM下功能的实现.这是项目中一个快捷键功能的扩展. 1, ...
- 使用RadioGroup与RadioButton实现多选一
RadioGroup是RadioButton的集合, RadioGroup里面可以包含很多RadioButton,提供多选一机制,只能选择其中一个 RadioGroup的orientation(方向) ...
- 安卓开发:UI组件-RadioButton和复选框CheckBox
2.5RadioButton 为用户提供由两个及以上互斥的选项组成的选项集. 2.5.1精简代码 在按钮变多之后,多次重复书写点击事件有些繁琐,我们在这里创建一个事件OnClick,每次点击时调用该事 ...
- Android 单选按钮(RadioButton)和复选框(CheckBox)的使用
1.RadioButton (1)介绍 (2)单选按钮点击事件的用法 (3)RadioButton与RadioGroup配合使用实现单选题功能 (4)xml布局及使用 <?xml version ...
- Qt Quick 常用元素:RadioButton(单选框),CheckBox(复选框) 与 GroupBox(分组框)
先介绍一下 ExclusiveGroup. ExclusiveGroup (互斥分组)本身是不可见元素,用于将若干个可选择元素组合在一起, 供用户选择其中的一个选项.你可以在 ExclusiveGro ...
- Android的RadioButton和checkBox的用法-android学习之旅(十九)
RadioButton和checkBox简介 单选按钮(RadioButton)和复选框(CheckBox)都继承了Button,因此属性的设置和Button差不多,只是加了一个android:che ...
- Android开发:文本控件详解——RadioButton和CheckBox(一)基本属性
一.RadioButton和RadioGroup: RadioButton是单个的圆形单选框,而RadioGroup是可以容纳多个RadioButton存在的容器,因此RadioButton和Radi ...
- Android笔记——Android自定义控件
目录: 1.自定义控件概述 01_什么是自定义控件 Android系统中,继承Android系统自带的View或者ViewGroup控件或者系统自带的控件,并在这基础上增加或者重新组合成我们想要的效果 ...
- ASP.net 常用服务器控件
新人初学,有错请指,大神轻喷. .net中有HTML控件和标准服务器控件. 老师教学用的是vs2010是这样分的,不知道15里是不是这样. 如果使用HTML控件我们就会发现页面中加载了原本HTML代码 ...
随机推荐
- Powershell ——findstr
从文件中找出关键字 $colItems = Get-ChildItem d:\test #定义文件夹的路径 foreach ($i in $colItems) #循环获取文件夹下的txt文件 { $f ...
- Phonetic Symbols&Rules of Pronunciation
音标 Phonetic Symbols http://yinbiao.tingclass.net/ 1.1元音 1.1.元音:元音有20个,其中单元音12个,双元音8个. (1)“短”单元音 [i] ...
- Django HttpRequest对象详解
WSGIRequest对象 Django在接收到http请求之后,会根据http请求携带的参数以及报文信息创建一个WSGIRequest对象,并且作为视图函数第一个参数传给视图函数.也就是我们经常看到 ...
- golang的多协程实践
go语言以优异的并发特性而闻名,刚好手上有个小项目比较适合. 项目背景: 公司播控平台的数据存储包括MySQL和ElasticSearch(ES)两个部分,编辑.运营的数据首先保存在MySQL中,为了 ...
- django settings相关配置
settings """ Django settings for mysite project. Generated by 'django-admin startproj ...
- smtplib与email模块(实现邮件的发送)
SMTP是发送邮件的协议,Python内置对SMTP的支持,可以发送纯文本邮件.HTML邮件以及带附件的邮件. Python对SMTP支持有smtplib和email两个模块,email负责构造邮件, ...
- 关于source insight、添加.s和.S文件,显示全部路径、加入项目后闪屏幕
1.source insight使用也有一年多时间了,今天出现建工程后添加文件“no files found” 百思不得姐: 后面发现是原工程命名时出现非法字符.重新命名就ok了. 切记切记 2.实用 ...
- python1变量,表达式和语句
1.变量和类型 变量是指向各种类型值的名字,以后再用到某个值时,直接引用这个名字即可,不用再写具体的值,在python中,变量的使用环境非常宽松,没有明显的变量声明,而且类型不是固定的.如果你不能确定 ...
- JSP使用网站访问人数统计功能,方法与技巧
实现网站访问人数统计功能的步骤: 创建静态登录页面,并指定表单提交由登录处理页面进行处理. 创建登录处理页面获得登录信息,查询数据库,判断该用户是否注册,如果该用户已注册,把已登录用户的信息保存在一个 ...
- 移动app自动化测试
原文出处https://www.toutiao.com/i6473606106970063374/ 原文作者是今日头条的:一个字头的诞生 在此感谢原文作者的无私分享! 移动App自动化测试(一) 目前 ...