Android学习笔记之RadioButton(RadioGroup)

RadioButton(单选按钮)在Androi发中应用的非常广泛,比如一些选择项的时候,会用到单选按钮。它是一种单个圆形单选框双状态的按钮,可以选择或不选择。在RadioButton没有被选中时,用户能够按下或点击来选中它。但是,与复选框相反,用户一旦选中就不能够取消选中。

    实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听

main.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:id="@+id/sex"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/sex" />  
  12.     <RadioGroup   
  13.         android:id="@+id/radiogroup"  
  14.         android:layout_width="fill_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:orientation="vertical"  
  17.         >  
  18.         <RadioButton   
  19.             android:layout_width="wrap_content"  
  20.             android:layout_height="wrap_content"  
  21.             android:id="@+id/female"  
  22.             android:text="@string/female"  
  23.             ></RadioButton>  
  24.         <RadioButton   
  25.             android:id="@+id/male"  
  26.             android:layout_width="wrap_content"  
  27.             android:layout_height="wrap_content"  
  28.             android:text="@string/male"  
  29.             android:checked="true"  
  30.             ></RadioButton>  
  31.     </RadioGroup>  
  32.   
  33. </LinearLayout>  


 

RadioGroupActivity.java

通过控件的ID来得到代表控件的对象

然后为RadioGroup设置监听器

  1. package Android.Activity;  
  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.Toast;  
  8.   
  9. public class RadioGroupActivity extends Activity {  
  10.     /** Called when the activity is first created. */  
  11.     private RadioButton maleButton = null;  
  12.     private RadioButton femaleButton = null;  
  13.     private RadioGroup radiogroup =null;  
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         super.onCreate(savedInstanceState);  
  17.         setContentView(R.layout.main);  
  18.         //通过控件的ID来得到代表控件的对象  
  19.         maleButton = (RadioButton)findViewById(R.id.male);  
  20.         femaleButton = (RadioButton)findViewById(R.id.female);  
  21.         radiogroup = (RadioGroup)findViewById(R.id.radiogroup);  
  22.         //为RadioGroup设置监听器,需要注意的是,这里的监听器和Button控件的监听器有所不同  
  23.         radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
  24.               
  25.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  26.                 // TODO Auto-generated method stub  
  27.                 if(femaleButton.getId() == checkedId){  
  28.                     Toast.makeText(RadioGroupActivity.this"female", Toast.LENGTH_SHORT).show();  
  29.                 }  
  30.                 else if(maleButton.getId() == checkedId){  
  31.                     Toast.makeText(RadioGroupActivity.this"male", Toast.LENGTH_SHORT).show();  
  32.                 }  
  33.             }  
  34.         });  
  35.     }  
  36. }  


 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值