单选按钮RadioButton

本文通过一个具体的 Android 开发实例,介绍了如何使用 RadioGroup 控件并实现单选按钮的值获取功能。文中提供了两种获取选中值的方法:一种是在 RadioGroup 的值改变时触发;另一种是在点击其他按钮时获取。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

先来个例子感受一下

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="fill_parent"
    android:layout_height="fill_parent" tools:context=".MainActivity"
    android:orientation="horizontal"
    >
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="性别:"
        android:height="50px"/>
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:orientation="horizontal"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio0"
            android:text="男"
            android:checked="true"/>
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/radio1"
            android:text="女"/>
    </RadioGroup>
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/button1"
        android:text="提交"/>
</LinearLayout>

有图有真相



1、在改变单选按钮组的值时获取

RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r = (RadioButton)findViewById(checkedId);
                r.getText(); //获取被选中的单选按钮的值
            }
        });

2、单击其他按钮时获

final RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
        Button button = (Button)findViewById(R.id.button1);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<sex.getChildCount();i++){
                    RadioButton r = (RadioButton)sex.getChildAt(i); //根据索引值获取单选按钮
                    if(r.isChecked()){  //判断单选按钮是否被选中
                        r.getText();    //获取被选中的单选按钮的值
                        break;          //跳出for循环
                    }
                }
            }
        });


实例上的java代码

protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        final RadioGroup sex = (RadioGroup)findViewById(R.id.radioGroup1);
        sex.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                RadioButton r = (RadioButton)findViewById(checkedId);//获取被选择的单选按钮
                Log.i("单选按钮","您选择的是:"+r.getText());
            }
        });
        Button button = (Button)findViewById(R.id.button1); //获取提交按钮
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                for(int i=0;i<sex.getChildCount();i++){
                    RadioButton r = (RadioButton)sex.getChildAt(i);
                    if(r.isChecked()){  //判断单选按钮是否被选中
                        Log.i("单选按钮","性别:"+r.getText());
                        break; //跳出for循环
                    }
                }
            }
        });

    }




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值