RadioButon 获取单选按钮组选中值的两种方法!

在单选按钮中我们一般需要获得当前用户选择的按钮值是什么,想要得到用户的选择有两种方法。

第一种:在改变单选按钮组的值时获取。在改变单选按钮的值时获取选中项的值时,首先需要获取单选按钮组,然后为其添加OnCheckedChangeListener,并在onCheckedChanged()方法中根据参数checkedId获取被选中的单选按钮,并通过其getText()方法获取该单选按钮对应的值;

第二种:单击其他按钮时获取。首先需要在该按钮的单击事件的监听器的onClick()方法中,通过for循环语句遍历当前按钮组,并根据被遍历到的单选按钮的isChecked()方法判断该按钮是否被选中,当被选中时,通过单选按钮的getText()方法获取对应的值。

xml代码:定义一个RadioGroup组里面放入两个RadioButton,一个提交按钮;

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <TextView 
        android:id="@+id/textView01"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:textSize="24sp"
        android:text="您的性别是?"
        />
    <RadioGroup 
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        >
        
        <RadioButton 
            android:id="@+id/radioButton01"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:checked="true"
            android:text="男"
            />
        
           <RadioButton 
            android:id="@+id/radioButton02"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="20sp"
            android:text="女"
            />
    </RadioGroup>

    <Button 
        android:id="@+id/submit"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="  提    交   "
        />
</LinearLayout>
JAVA代码:

package com.example.radiobutton;

import android.os.Bundle;
import android.R.integer;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
       /**
        * 1、在改变单选按钮组的值时获取。在改变单选按钮的值时获取选中项的值时,首先需要获取单选按钮组,
        * 然后为其添加OnCheckedChangeListener,并在onCheckedChanged()方法中根据参数checkedId获取
        * 被选中的单选按钮,并通过其getText()方法获取该单选按钮对应的值
        */
//        RadioGroup sexGroup = (RadioGroup)findViewById(R.id.radioGroup1);
//        sexGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {
//			
//			@Override
//			public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
//				// TODO Auto-generated method stub
//				RadioButton radioButton = (RadioButton)findViewById(checkedId);
//				Log.i("tag", "lsn 单选按钮,您的性别是:"+radioButton.getText());//获取被选中的单选按钮的值
//			}
//		});
        
        /**
         * 2、单击其他按钮时获取
         * 首先需要在该按钮的单击事件的监听器的onClick()方法中,
         * 通过for循环语句遍历当前按钮组,并根据被遍历到的单选按钮的isChecked()方法
         * 判断该按钮是否被选中,当被选中时,通过单选按钮的getText()方法获取对应的值。
         */
        final RadioGroup sexRadioGroup = (RadioGroup)findViewById(R.id.radioGroup1);
        Button btnSubmit = (Button)findViewById(R.id.submit);
        btnSubmit.setOnClickListener(new OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				for(int i=0;i<sexRadioGroup.getChildCount();i++){
					RadioButton radioButton = (RadioButton)sexRadioGroup.getChildAt(i);
					if(radioButton.isChecked()){
        Log.i("tag", "lsn 单选按钮,您的性别是:"+radioButton.getText());
					}
				}
			}
		});
    }

    
}
在logCat中看到的结果如下:






  • 16
    点赞
  • 39
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值