Android进阶之RadioButton选中值的获取
来源:互联网 发布:淘宝详情页ps技巧 编辑:程序博客网 时间:2024/06/05 23:40
RadioButton选中值的获取
首先RadioButton是嵌套在RadioGroup中的,即一个RadioGoup中可以拥有多个RadioButton,但是一般至少是两个。而在布局文件中,RadioButton外部嵌套RadioGroup,因此在获取RadioButton的选中值时应该先获取当前RadioGroup,利用RadioGroup得到用户已经选中的RadioButton,这样就可以利用getText()方法获取选中内容。
第一种方式:
通过radioGroup.getCheckedRadioButtonId()来得到选中的RadioButton的ID,从而利用findviewbyid得到RadioButton进而获取选中值
1、布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="wf.com.radiobuttondemo.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别是:" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioMan" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="男" /> <RadioButton android:id="@+id/radioWonan" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="女" /> </RadioGroup> <TextView android:id="@+id/txt_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别是:" /></LinearLayout>
public class MainActivity extends Activity { @BindView(R.id.radioMan) RadioButton radioMan; @BindView(R.id.radioWonan) RadioButton radioWonan; @BindView(R.id.radioGroup) RadioGroup radioGroup; @BindView(R.id.txt_sex) TextView txtSex; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this);
//通过RadioGroup的setOnCheckedChangeListener()来监听选中哪一个单选按钮 radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() { @Override public void onCheckedChanged(RadioGroup group, int checkedId) { selectRadioButton(); } }); } private void selectRadioButton() {
//通过radioGroup.getCheckedRadioButtonId()来得到选中的RadioButton的ID,从而得到RadioButton进而获取选中值 RadioButton rb = (RadioButton)MainActivity.this.findViewById(radioGroup.getCheckedRadioButtonId()); txtSex.setText(rb.getText()); }}
需要利用一下三个方法
(1)radiogroup.getChildCount() 获取radiogroup中子组件(radioButton)的数目
(2)radiogroup.getChildAt() 根据索引获取当前索引对应的radioButton
(3)radiobutton.isChecked() 判断当前组件是否被选中
整体思路是,对radiogroup中组件进行循环,依次判断isChecked(),从而找到选中的组件
1、布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/activity_main" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" tools:context="wf.com.radiobuttondemo.MainActivity"> <TextView android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别是:" /> <RadioGroup android:id="@+id/radioGroup" android:layout_width="match_parent" android:layout_height="wrap_content" > <RadioButton android:id="@+id/radioMan" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="男" /> <RadioButton android:id="@+id/radioWonan" android:layout_height="wrap_content" android:layout_width="match_parent" android:text="女" /> </RadioGroup> <TextView android:id="@+id/txt_sex" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="性别是:" /> <Button android:id="@+id/btn_submit" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="提交" /></LinearLayout>
public class MainActivity extends Activity { @BindView(R.id.radioMan) RadioButton radioMan; @BindView(R.id.radioWonan) RadioButton radioWonan; @BindView(R.id.radioGroup) RadioGroup radioGroup; @BindView(R.id.txt_sex) TextView txtSex; @BindView(R.id.btn_submit) Button btnSubmit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this); } @OnClick(R.id.btn_submit) public void onClick() { for(int i = 0 ;i < radioGroup.getChildCount();i++){ RadioButton rb = (RadioButton)radioGroup.getChildAt(i); if(rb.isChecked()){ txtSex.setText(rb.getText()); break; } } }}
1 0
- Android进阶之RadioButton选中值的获取
- 获取选中radiobutton的值
- jquery获取radiobutton的选中值
- Android 如何获取RadioGroup选中RadioButton的内容
- Android 如何获取RadioGroup选中RadioButton的内容
- Android 如何获取RadioGroup选中RadioButton监听
- 得到选中的radiobutton值
- Jquery 如何 获取dropdownlist、RadioButton 选中值
- Android中radioButton的默认选中
- 关于获取RadioGroup里面的RadioButton的值,多行RadioGroup展示清除选中效果
- 关于获取RadioGroup里面的RadioButton的值,多行RadioGroup展示清除选中效果
- QT中获取选中的radioButton的两种方法
- QT中要获取radioButton组中被选中的那个按钮
- radiobutton赋选中值
- 自定义RadioGroup动态添加RadioButton,并获取选中radioButton的位置
- android radiobutton 设置选中问题
- android radiobutton 设置选中问题
- Android 动态设置RadioButton选中和没选中的颜色及设置默认选中某一项
- wpf 模板内控件在后台CS文件里面获取属性值
- 清除浮动最好的两种方式
- spring boot(六):如何优雅的使用mybatis
- keytool生成keystore、truststore、证书
- TensorFlow学习笔记(1)--TensorFlow简介,常用基本操作
- Android进阶之RadioButton选中值的获取
- Makefile基础
- iOS扫描二维码与生成二维码
- JDK,JRE,JVM区别与联系
- C#与工业控制-串口下
- 《跟我一起写makefile》(笔记8--makefile中的隐含规则#模式规则#隐含规则变量#自动化变量)
- linux批量替换文件名
- Freeline 让AndroidStudio快的飞起来
- daemon守护进程初识