android 入门 radioGroup

package com.zte.android.lession;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;

public class Activity_radio_004 extends Activity
{
	private TextView txt ;
	private RadioGroup radioGroup ;
	private RadioButton radioBtn1, radioBtn2;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.radiogroup_layout_004);
		//实例化组件
		txt = (TextView)findViewById(R.id.l004_textView1);
		radioGroup = (RadioGroup)findViewById(R.id.lession004_sex_radioGroup1);
		radioGroup.setOnCheckedChangeListener(radioGroupOnCheckedChangeListener);
		//未分组radiobutton
		radioBtn1 = (RadioButton)findViewById(R.id.l004_radioButton3);
		//未分组radiobutton 监听事件
		radioBtn1.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub id=l004_radioButton3
				//关于 charsequence
				/**
				 * Strings are CharSequences, 
				 * so you can just use Strings and not worry. 
				 * Android is merely trying to be helpful by allowing you 
				 * to also specify other CharSequence objects, like StringBuffers
				 */
				CharSequence rd3 = radioBtn1.getText();
				txt.setText("......."+rd3);
			}
		});
		
		// 未分组radioBtn2 属性判断
		radioBtn2 = (RadioButton)findViewById(R.id.l004_radioButton4);
		radioBtn2.setOnClickListener(new OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				if(radioBtn2.isChecked()){
					txt.setText("......."+radioBtn2.getText());
				}
			}
		});
		
	}
	
	//radioGroupOnCheckedChangeListener	
	private OnCheckedChangeListener radioGroupOnCheckedChangeListener = 
			new OnCheckedChangeListener() {
		@Override
		public void onCheckedChanged(RadioGroup group, int checkedId) {
			// TODO Auto-generated method stub
			if(R.id.lession004_radio0 == checkedId){
				txt.setText("你选择了男性....");
			}else{
				txt.setText("你选择了女性....");
			}
		}
	};
	
}
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/l004_textView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="" />

    <RadioGroup
        android:id="@+id/lession004_sex_radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/lession004_radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="male" />

        <RadioButton
            android:id="@+id/lession004_radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="female" />

    </RadioGroup>
	
    <!-- 未分组的 radiobutton -->
    <RadioButton
        android:id="@+id/l004_radioButton3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="未分组radiobutton 1" />

    <RadioButton
        android:id="@+id/l004_radioButton4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="未分组radiobutton 2" />

    <!-- 把radiobutton放到一排 android:orientation -->
    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="R1" />

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="R2" />

        <RadioButton
            android:id="@+id/radio2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="R3" />

    </RadioGroup>

</LinearLayout>

相关推荐