Android RadioGroup和RadioButton使用

 

Android RadioGroup和RadioButton使用

分类: Android   1970人阅读  评论(1)  收藏  举报

RadioGroup管理一组RadioButton,其中的RadioButton只能做出单一选择。

效果如:

temp_thumb1

先给出整个Activity的布局文件main.xml:


   
   
[html] view plain copy
  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.     <<>TextView  
  7.         android:id="@+id/radiobutton_textview"  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="50dip"  
  10.         android:textSize="18dip"  
  11.         android:textStyle="bold"  
  12.         android:background="@android:drawable/title_bar"  
  13.         android:textAppearance="?android:attr/textAppearanceLarge"  
  14.         android:gravity="center_vertical"  
  15.         />  
  16.     <<>RadioGroup  
  17.         android:id="@+id/group"  
  18.         android:layout_width="fill_parent"  
  19.         android:layout_height="wrap_content"  
  20.         android:orientation="vertical">  
  21.         <<>RadioButton  
  22.             android:id="@+id/button1"  
  23.             android:layout_width="fill_parent"  
  24.             android:layout_height="50dip"  
  25.             android:textSize="20dip"  
  26.             android:paddingLeft="30dip"  
  27.             android:text="Android新手"  
  28.             android:button="@null"                      
  29.             android:drawableRight="@android:drawable/btn_radio"/>  
  30.         <<>View  
  31.             android:layout_width="fill_parent"  
  32.             android:layout_height="1px"  
  33.             android:background="?android:attr/listDivider"  
  34.             />  
  35.         <<>RadioButton  
  36.             android:id="@+id/button2"  
  37.             android:layout_width="fill_parent"  
  38.             android:layout_height="50dip"  
  39.             android:textSize="20dip"  
  40.             android:paddingLeft="30dip"  
  41.             android:text="Android高手"  
  42.             android:button="@null"  
  43.             android:drawableRight="@android:drawable/btn_radio"/>  
  44.         RadioGroup>      
  45. LinearLayout>  

RadioButton 默认按钮在文本的左边,我放到右边,涉及代码:android:button="@null",将左侧的按钮消除;

android:drawableRight="@android:drawable/btn_radio",在文本右侧添加按钮。

MainActivity主要介绍 单选按钮组监听事件:OnCheckedChangeListener


   
   
[java] view plain copy
  1. /** 
  2.  * @ 
  3.  * @author ahutzh 
  4.  * @data 2011-4-14 
  5.  */  
  6. public class MainActivity extends Activity {  
  7.     private TextView textView;  
  8.     private RadioGroup group;  
  9.    /** Called when the activity is first created. */  
  10.       @Override  
  11.     public void onCreate(Bundle savedInstanceState) {  
  12.         super.onCreate(savedInstanceState);  
  13.             setContentView(R.layout.main);  
  14.   
  15.         textView = (TextView) findViewById(R.id.radiobutton_textview);  
  16.         group = (RadioGroup) findViewById(R.id.group);  
  17.   
  18.         // 单选按钮组监听事件  
  19.         group.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  20.   
  21.             @Override  
  22.                 public void onCheckedChanged(RadioGroup group, int checkedId) {  
  23.                 // 根据ID判断选择的按钮  
  24.                     if (checkedId == R.id.button1) {  
  25.                         textView.setText("Android新手");  
  26.                     } else {  
  27.                             textView.setText("Android高手");  
  28.                     }  
  29.                 }  
  30.           });  
  31.     }  
  32. }  
RadioGroup注册监听事件OnCheckedChangeListener(),在onCheckedChanged实现业务逻辑。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值