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); } }
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
· 浏览器原生「磁吸」效果!Anchor Positioning 锚点定位神器解析
· 全程不用写代码,我用AI程序员写了一个飞机大战
· DeepSeek 开源周回顾「GitHub 热点速览」
· 记一次.NET内存居高不下排查解决与启示
· MongoDB 8.0这个新功能碉堡了,比商业数据库还牛
· .NET10 - 预览版1新功能体验(一)
2014-12-02 使用NPOI将DataTable生成Excel
2013-12-02 C# for AUTOCAD ActiveX获取图形对象坐标程序
2010-12-02 二调,分幅图打印(标准分幅土地利用现状图,耕地坡度分级图),图框工具
2010-12-02 二调,分幅图打印 边框注记距离内图框1毫米的程序
2010-12-02 通过ArcEngine获得注记的信息
2010-12-02 ArcEngine 获得注记文字的大小
2010-12-02 arcgis 平台实现字体左斜