RadioGroup组与onCheckedChanged事件

效果图:

 

Radio

 

第一步:建立Android工程 Test6

 

第二步:编写string.xml

 

Xml代码   收藏代码
  1. <string name="app_name">RadioApp</string>  
  2. <string name="tr_radio_op1">帅哥</string>  
  3. <string name="tr_radio_op2">美女</string>  
  4. <string name="str_radio_question1">请问你是?</string>  

 

 

第三步:编写main.xml

 

Xml代码   收藏代码
  1. <TextView   
  2.         android:id="@+id/myTextView"   
  3.         android:layout_width="fill_parent"  
  4.         android:layout_height="49px"                               
  5.         android:text="@string/str_radio_question1"  
  6.         android:textSize="30sp" />  
  7.     <!-- 建立一个RadioGroup -->  
  8. <RadioGroup   
  9.         android:id="@+id/myRadioButton"  
  10.         android:layout_width="137px"   
  11.         android:layout_height="216px"  
  12.         android:orientation="vertical">  
  13.        <!-- 第一个RadioButton -->  
  14.        <RadioButton   
  15.                android:id="@+id/myRadioButton1"  
  16.                android:layout_width="wrap_content"    
  17.                android:layout_height="wrap_content"  
  18.                android:text="@string/tr_radio_op1" />  
  19.        <!--  第二个RadioButton-->  
  20.        <RadioButton   
  21.                android:id="@+id/myRadioButton2"  
  22.                android:layout_width="wrap_content"     
  23.                android:layout_height="wrap_content"  
  24.                android:text="@string/tr_radio_op2" />  
  25. </RadioGroup>  

 

 

第三步:编写Activity子类 RadioDemo

 

Java代码   收藏代码
  1. package com.android;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.widget.RadioButton;  
  6. import android.widget.RadioGroup;  
  7. import android.widget.TextView;  
  8. import android.widget.Toast;  
  9. import android.widget.RadioGroup.OnCheckedChangeListener;  
  10.   
  11. public class RadioDemo extends Activity {  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         final TextView tv=(TextView)findViewById(R.id.myTextView);  
  19.           
  20.         RadioGroup rg=(RadioGroup)findViewById(R.id.myRadioButton);  
  21.           
  22.         final RadioButton rb1=(RadioButton)findViewById(R.id.myRadioButton1);  
  23.           
  24.         final RadioButton rb2=(RadioButton)findViewById(R.id.myRadioButton2);  
  25.           
  26.         rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){  
  27.   
  28.             public void onCheckedChanged(RadioGroup arg0, int checkedId) {  
  29.                   
  30.                 if(checkedId==rb1.getId()){  
  31.                       
  32.                     tv.setText("您选在的是:"+rb1.getText().toString());    
  33.                       
  34.                 }  
  35.                   
  36.                 if(checkedId==rb2.getId()){  
  37.                       
  38.                     tv.setText("您选择的是:"+rb2.getText().toString());  
  39.                       
  40.                 }  
  41.                   
  42.             }  
  43.               
  44.         });  
  45.     }  
  46. }  
package com.android;

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

public class RadioDemo extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        
        final TextView tv=(TextView)findViewById(R.id.myTextView);
        
        RadioGroup rg=(RadioGroup)findViewById(R.id.myRadioButton);
        
        final RadioButton rb1=(RadioButton)findViewById(R.id.myRadioButton1);
        
        final RadioButton rb2=(RadioButton)findViewById(R.id.myRadioButton2);
        
        rg.setOnCheckedChangeListener(new OnCheckedChangeListener(){

			public void onCheckedChanged(RadioGroup arg0, int checkedId) {
				
				if(checkedId==rb1.getId()){
					
					tv.setText("您选在的是:"+rb1.getText().toString());	
					
				}
				
				if(checkedId==rb2.getId()){
					
					tv.setText("您选择的是:"+rb2.getText().toString());
					
				}
				
			}
        	
        });
    }
}

 

 

http://sraining.iteye.com/blog/726666
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值