现在的位置: 首页 > 综合 > 正文

RadioGroup实现单选并获得所选项值

2013年10月27日 ⁄ 综合 ⁄ 共 4505字 ⁄ 字号 评论关闭

上面是一个TextView,下面有个RadioGroup,布局如下:

主布局 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">
    <!--第一个TextView -->
    <TextView
        android:id="@+id/myTextView"
        android:layout_width="228px"
        android:layout_height="49px"
        android:text="@string/str_radio_question1"
        android:textSize="30sp"
        />
    <!--建立一个RadioGroup -->
    <RadioGroup
        android:id="@+id/myRadioGroup"
        android:layout_width="137px"
        android:layout_height="216px"
        android:orientation="vertical">
        <!--第一个RadioButton -->
        <RadioButton
            android:id="@+id/myRadioButton1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tr_radio_op1"
            />
        <!--第二个RadioButton -->
        <RadioButton
            android:id="@+id/myRadioButton2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/tr_radio_op2"
            />
    </RadioGroup>
</LinearLayout>

 

主控制程序 RadioGroupDemo.java

package com.android.test; 
   
import android.app.Activity; 
import android.os.Bundle; 
import android.widget.RadioButton; 
import android.widget.RadioGroup; 
import android.widget.TextView; 
   
public class RadioGroupDemo extends Activity { 
    public TextView mTextView1; 
    public RadioGroup mRadioGroup1; 
    public RadioButton mRadio1, mRadio2; 
   
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
   
        // 取得 TextView、RadioGroup、RadioButton对象 
        mTextView1 = (TextView) findViewById(R.id.myTextView); 
        mRadioGroup1 = (RadioGroup) findViewById(R.id.myRadioGroup); 
        mRadio1 = (RadioButton) findViewById(R.id.myRadioButton1); 
        mRadio2 = (RadioButton) findViewById(R.id.myRadioButton2); 
   
        // RadioGroup用OnCheckedChangeListener来运行 
        mRadioGroup1.setOnCheckedChangeListener(mChangeRadio); 
    } 
   
    private RadioGroup.OnCheckedChangeListener mChangeRadio = new RadioGroup.OnCheckedChangeListener() { 
        @Override
        public void onCheckedChanged(RadioGroup group, int checkedId) { 
            // TODO Auto-generated method stub 
            if (checkedId == mRadio1.getId()) { 
                // 把mRadio1的内容传到mTextView1 
                mTextView1.setText(mRadio1.getText()); 
            } else if (checkedId == mRadio2.getId()) { 
                // 把mRadio2的内容传到mTextView1 
                mTextView1.setText(mRadio2.getText()); 
            } 
        } 
    }; 
} 

1 需要注意的就是RadioGroup的消息处理(响应事件处理),跟Button的有些不同。
    mRadioGroup1.setOnCheckedChangeListener(mChangeRadio);

2 RadioGroup注册监听事件OnCheckedChangeListener(),在onCheckedChanged()方法里实现业务逻辑。

3

 

RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

2、每个RadioGroup中的RadioButton同时只能有一个被选中

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

4、大部分场合下,一个RadioGroup中至少有2个RadioButton

5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

6、<RadioButtonandroid:layout_width="wrap_content" android:layout_height="wrap_content"
android:id
="@+id/radioMale" android:text="男" android:checked="true"></RadioButton>

 

 

选中项变更的事件监听:

当RadioGroup中的选中项变更后,您可能需要做一些相应,比如上述例子中,性别选择“女”后下面的本文也相应改变,又或者选择不同的性别后,出现符合该性别的头像列表进行更新,女生不会喜欢使用大胡子作为自己的头像。

XML布局:

<?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
 <TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="请选择您的性别:"
    android:textSize="9pt"
    />
 <RadioGroup android:id="@+id/radioGroup" android:contentDescription="性别" android:layout_width="wrap_content" android:layout_height="wrap_content">
    <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioMale" android:text="男" android:checked="true"></RadioButton>
    <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="@+id/radioFemale" android:text="女"></RadioButton>    
 </RadioGroup>
<TextView  
    android:id="@+id/tvSex"
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="您的性别是:男"
    android:textSize="9pt"
    />
</LinearLayout>


 

后台代码如下:

TextView tv = null;//根据不同选项所要变更的文本控件
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.main);
        
        //根据ID找到该文本控件
        tv = (TextView)this.findViewById(R.id.tvSex);
        //根据ID找到RadioGroup实例
        RadioGroup group = (RadioGroup)this.findViewById(R.id.radioGroup);
        //绑定一个匿名监听器
        group.setOnCheckedChangeListener(new OnCheckedChangeListener() {
            
            @Override
            public void onCheckedChanged(RadioGroup arg0, int arg1) {
                // TODO Auto-generated method stub
                //获取变更后的选中项的ID
                int radioButtonId = arg0.getCheckedRadioButtonId();
                //根据ID获取RadioButton的实例
                RadioButton rb = (RadioButton)MyActiviy.this.findViewById(radioButtonId);
                //更新文本内容,以符合选中项
                tv.setText("您的性别是:" + rb.getText());
            }
        });
    }

 

抱歉!评论已关闭.