栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

如何获取RadioGroup中RadioButton的值?

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

如何获取RadioGroup中RadioButton的值?


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,整体的使用样例:

布局文件xml中的内容:


 
 
    

代码实现:

    private RadioGroup mSex_group;
    private RadioButton mMale;
    private RadioButton mFemale;
    private String sexName;
    mSex_group = (RadioGroup) findViewById(R.id.sex_group);
 mMale = (RadioButton) findViewById(R.id.male);
 mFemale = (RadioButton) findViewById(R.id.female);
   mSex_group.setonCheckedChangeListener(new RadioGroup.onCheckedChangeListener() {
     @Override
     public void onCheckedChanged(RadioGroup group, int checkedId) {
  if (mMale.getId() == checkedId) {
      sexName = mMale.getText().toString();
  } else if (mFemale.getId() == checkedId) {
      sexName = mFemale.getText().toString();
  }
     }
 });

----------

如有错误,欢迎指教!谢谢阅读
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/233656.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号