4.6 RadioButton与RadioGroup的使用

package com.chaowen;

import javax.security.auth.PrivateCredentialPermission;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

public class Ex04_06_RadioGroup extends Activity {
    /** Called when the activity is first created. */
	private TextView mTextView;
	private RadioGroup radioGroup;
	private RadioButton radioButton1,radioButton2;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        
        mTextView=(TextView)findViewById(R.id.myTextView);
        radioButton1=(RadioButton)findViewById(R.id.myRadioButton1);
        radioButton2=(RadioButton)findViewById(R.id.myRadioButton2);
        radioGroup=(RadioGroup)findViewById(R.id.myRadioGroup);
        
       
        
        RadioGroup.OnCheckedChangeListener mChangeRadio=new RadioGroup.OnCheckedChangeListener() {
			
			@Override
			public void onCheckedChanged(RadioGroup group, int checkedId) {
				if(checkedId==radioButton1.getId()){
					//把radioButton的内容传到mTextview1
					mTextView.setText(radioButton1.getText());
				}else if(checkedId==radioButton2.getId()){
					//把mRadio2的内容传到mTextView1
					mTextView.setText(radioButton2.getText());
				}
				
			}
		};
		
		 //RadioGroup用OnCheckedChangeListener来运行
        radioGroup.setOnCheckedChangeListener(mChangeRadio);
    }
}

  main.xml

<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout 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"    
    android:layout_x="37px"
    android:layout_y="3px"
  />   
  <!--建立一個RadioGroup -->
  <RadioGroup
    android:id="@+id/myRadioGroup"
    android:layout_width="137px"
    android:layout_height="216px"
    android:orientation="vertical"
    android:layout_x="3px"
    android:layout_y="54px" 
    >
    <!--第一個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>  
</AbsoluteLayout>
 

  Strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">Hello World, Ex04_06_RadioGroup!</string>
    <string name="app_name">Ex04_06_RadioGroup</string>
    <string name="tr_radio_op1">帥哥</string>
  <string name="tr_radio_op2">美女</string>
  <string name="str_radio_question1">請問你是?</string> 
</resources>
 

 


 

 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值