Android进阶之RadioButton选中值的获取

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>

2、MainActivity
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>

2、MainActivity
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;
            }
        }
    }
}



   



  • 11
    点赞
  • 33
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值