Android 学习笔记---获取RadioGroup的选定值
1,获取RadioGroup控件: RadioGroup radioGroup = (RadioGroup)findViewById(R.id.myRadioGroup);
2,获取RadioButton控件; RadioButton radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId());
3,获取选中的radio的值: String text = radioButton.getText().toString();
4,为radioGroup添加监听事件,用来监听组件内部的事件响应: radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { //在这个函数里面用来改变选择的radioButton的数值,以及与其值相关的 //任何操作,详见下文 selectRadioBtn(); } });
5,在onCreat中需要初始化上面的四条信息;
6,整体的使用样例: protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.waterselect); getViews(); setListenerForView(); }
private void getViews(){ //获取库内上水的radio组件信息、 radioGroup = (RadioGroup)findViewById(R.id.isWaterByContent); }
private void setListenerForView(){ //选择radio selectRadioBtn(); //库内上水的监听事件 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { selectRadioBtn(); } }); }
private void selectRadioBtn(){ radioButton = (RadioButton)findViewById(radioGroup.getCheckedRadioButtonId()); waterIn = radioButton.getText().toString(); Log.i("radio", waterIn); }
来自:http://blog.sina.com.cn/s/blog_9c5364110101c1bj.html
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin" > <!-- 定义RadioGroup控件 ,代表政治面貌选择组 -->
<RadioGroup
android:id="@+id/radiogroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- 定义RadioButton控件 ,代表党员选项 -->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="党员" />
<!-- 定义RadioButton控件 ,代表群众选项 -->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="群众" />
<!-- 定义RadioButton控件 ,代表团员选项 -->
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="团员" />
</RadioGroup> <TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="结果"
android:id="@+id/yl1"></TextView>
<!--android:layout_gravity="center_horizontal" />--> </LinearLayout>
package com.example.yanlei.yl2; import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
//导入必备的包
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;
import android.widget.CompoundButton.OnCheckedChangeListener; public class MainActivity extends AppCompatActivity { private RadioGroup radiogroup1; //定义足球的复选框对象
private TextView yl1; //定义结果文本便签对象 @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); //调用父类的onCreate方法 //通过setContentView方法设置当前页面的布局文件为activity_main
setContentView(R.layout.activity_main);
findView(); //获取页面中的控件
setListener(); //设置控件的监听器
} private void setListener() {
// TODO Auto-generated method stub
//设置所有Radiogroup的状态改变监听器
radiogroup1.setOnCheckedChangeListener(mylistener); } RadioGroup.OnCheckedChangeListener mylistener=new RadioGroup.OnCheckedChangeListener()
{
@Override
public void onCheckedChanged(RadioGroup Group, int Checkid) {
// TODO Auto-generated method stub
//设置TextView的内容显示CheckBox的选择结果
setText();
}
}; private void findView() {
// TODO Auto-generated method stub
//通过findViewById得到对应的控件对象
radiogroup1 = (RadioGroup)findViewById(R.id.radiogroup1); yl1 = (TextView)findViewById(R.id.yl1);
} private void setText(){
String str;
yl1.setText(""); //清空TextView的内容
RadioButton radioButton = (RadioButton)findViewById(radiogroup1.getCheckedRadioButtonId()); int id= radiogroup1.getCheckedRadioButtonId();
str=radioButton.getText().toString();
yl1.setText("选择对象是:id="+id+",值:"+str); } }
Android 学习笔记---获取RadioGroup的选定值的更多相关文章
- Android学习笔记--获取传感器信息
相关资料: 传感器的坐标与读数:http://www.cnblogs.com/mengdd/archive/2013/05/19/3086781.html 传感器介绍及指南针原理:http://www ...
- [Android学习笔记]获取view的尺寸和坐标
对于UI方面很多时候需要获取它的很多信息,具体情况见view的文档 View文档 http://developer.android.com/training/index.html 常用方法:获取vie ...
- Android学习笔记-获取手机内存,SD卡存储空间。
前面介绍到如何保存数据到手机内存或者SD卡,但是问题是,在保存以前,我们还需要对他们的空间(可用空间),进行判断,才可以进行后续操作,所以,本节我们就介绍如何获取手机内存以及Sd卡的空间. //这时获 ...
- 【转】Pro Android学习笔记(三十):Menu(1):了解Menu
目录(?)[-] 创建Menu MenuItem的属性itemId MenuItem的属性groupId MenuItem的属性orderId MenuItem的属性可选属性 Menu触发 onOpt ...
- 【转】 Pro Android学习笔记(十九):用户界面和控制(7):ListView
目录(?)[-] 点击List的item触发 添加其他控件以及获取item数据 ListView控件以垂直布局方式显示子view.系统的android.app.ListActivity已经实现了一个只 ...
- Android 学习笔记之Volley(七)实现Json数据加载和解析...
学习内容: 1.使用Volley实现异步加载Json数据... Volley的第二大请求就是通过发送请求异步实现Json数据信息的加载,加载Json数据有两种方式,一种是通过获取Json对象,然后 ...
- android学习笔记36——使用原始XML文件
XML文件 android中使用XML文件,需要开发者手动创建res/xml文件夹. 实例如下: book.xml==> <?xml version="1.0" enc ...
- Android学习笔记之JSON数据解析
转载:Android学习笔记44:JSON数据解析 JSON(JavaScript Object Notation)是一种轻量级的数据交换格式,采用完全独立于语言的文本格式,为Web应用开发提供了一种 ...
- Android学习笔记36:使用SQLite方式存储数据
在Android中一共提供了5种数据存储方式,分别为: (1)Files:通过FileInputStream和FileOutputStream对文件进行操作.具体使用方法可以参阅博文<Andro ...
随机推荐
- 【Java_基础】Java的访问权限控制
1.类成员的访问权限控制 Java中类成员的访问权限分为四类:private,无(默认情况下),protected和public.其权限控制如下表所示: 修饰词 本类 同一个包的类 继承类 其他类 p ...
- Redis数据库(一)
1. Redis简介 Redis是非关系型数据库(nosql),数据保存在内存中,安全性低,但读取速度快. Redis主要存储变化较快且数据不是特别重要的数据. Redis是一个key-value存储 ...
- centos 7 中文乱码的解决办法
@@首先查看系统的操作版本,我的版本是centos 7.2 的. @@查看系统是否有安装中文语言包,一般我们在安装的时候系统都会默认的为我们安装上去的. locale -a | grep " ...
- 浏览器中如何获取想要的offsetwidth、、、clientwidth、、offsetheight、、、clientheight。。。
clientWidth是对象看到的宽度(不含边线,即border)scrollWidth是对象实际内容的宽度(若无padding,那就是边框之间距离,如有padding,就是左padding和右pad ...
- python爬虫入门三:requests库
urllib库在很多时候都比较繁琐,比如处理Cookies.因此,我们选择学习另一个更为简单易用的HTTP库:Requests. requests官方文档 1. 什么是Requests Request ...
- 对uboot中CFG_和CONFIG_的理解
CONFIG_用于选择CPU SOC 板子的类型,系统时钟,设备驱动driver驱动等 CFG_用于设置malloc缓冲池的大小,偏移地址部分的定义,uboot的提示符,uboot的加载地址,fl ...
- Verilog学习笔记基本语法篇(六)········ 循环语句
在Verilog中存在着4种类型的循环语句,用来控制执行语句的执行次数. 1)forever语句: 连续执行的语句. 2)repeat语句: 连续执行n次的语句. 3)while语句: 执行语 ...
- c#笔记2018-12-26
using System; /*C#学习笔记2018-12-26 * 1.@逐字字符串 * 2.数据类型转换 * 3.变量声明和占位符使用 * 4.接收用户输入值 * 5.const 关键字 * 6. ...
- [LA3620]Manhattan Wiring
[LA3620]Manhattan Wiring 试题描述 输入 输出 输入示例 输出示例 数据规模及约定 见“输入” 题解 我们把“连线”的过程改为“铺地砖”的过程,总共有 11 种地砖,每种地砖上 ...
- VM上完美运行macos
VM上完美运行macos 作者:方辰昱 时间:十月三号 效果图 简要步骤 下载安装VM 下载镜像文件链接,darwin.iso,unlocker,beamoff.合集下载链接:https://pan. ...