Android自定义RadioGroup实现单选完整示例

MainActivity如下:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package cc.testradiogroup;  
  2.   
  3. import android.os.Bundle;  
  4. import android.widget.RadioButton;  
  5. import android.widget.RadioGroup;  
  6. import android.widget.RadioGroup.OnCheckedChangeListener;  
  7. import android.app.Activity;  
  8. /** 
  9.  * Demo描述: 
  10.  * 利用自定义RadioGroup实现单选 
  11.  * 
  12.  * 参考资料: 
  13.  * 1 http://blog.csdn.net/xiaanming 
  14.  * 2 http://bbs.51cto.com/thread-954128-1.html 
  15.  *  
  16.  *   Thank you very much 
  17.  */  
  18. public class MainActivity extends Activity {  
  19.     private RadioGroup mRadioGroup;   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.         init();  
  25.     }  
  26.     
  27.     private void init(){  
  28.         mRadioGroup=(RadioGroup) findViewById(R.id.radioGroup);  
  29.         mRadioGroup.setOnCheckedChangeListener(new RadioButtonOnCheckedChangeListenerImpl());  
  30.     }  
  31.       
  32.     // 监听单选的变化  
  33.     private class RadioButtonOnCheckedChangeListenerImpl implements OnCheckedChangeListener {  
  34.         @Override  
  35.         public void onCheckedChanged(RadioGroup group, int checkedId) {  
  36.             RadioButton rb = (RadioButton) findViewById(group.getCheckedRadioButtonId());  
  37.             String currentSelected = rb.getText().toString();  
  38.             System.out.println("现在选中是:" + currentSelected);  
  39.         }  
  40.     }  
  41.   
  42. }  


main.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <!-- 步骤如下: -->  
  3. <!--1: android:button="@null" 去掉自带的图标   -->  
  4. <!--2:android:drawableRight="@drawable/line" 在文字的右边设置图片-->  
  5. <!--3: android:drawablePadding="10dip" 图片与文字间的距离-->  
  6. <!--4:android:layout_marginLeft="-17dip" 每个RadioButton距离左边缘或者距其左RadioButton的距离-->  
  7. <LinearLayout   
  8.     xmlns:android="http://schemas.android.com/apk/res/android"  
  9.     android:layout_width="fill_parent"  
  10.     android:layout_height="fill_parent"  
  11.     android:orientation="vertical"  
  12.     android:gravity="center_horizontal" >  
  13.   
  14.     <TextView  
  15.         android:layout_width="wrap_content"  
  16.         android:layout_height="wrap_content"  
  17.         android:textSize="15sp"  
  18.         android:text="测试自定义的RadioGroup来实现单选功能" >  
  19.     </TextView>  
  20.   
  21.     <RadioGroup  
  22.         android:id="@+id/radioGroup"  
  23.         android:layout_width="wrap_content"  
  24.         android:layout_height="wrap_content"  
  25.         android:orientation="horizontal"  
  26.         android:background="@drawable/bg" >  
  27.   
  28.         <RadioButton  
  29.             android:id="@+id/cai"  
  30.             android:layout_width="wrap_content"  
  31.             android:layout_height="wrap_content"  
  32.             android:layout_marginLeft="-17dip"  
  33.             android:checked="true"  
  34.             android:button="@null"  
  35.             android:drawableRight="@drawable/line"  
  36.             android:drawablePadding="10dip"  
  37.             android:text="菜"  
  38.             android:textColor="@drawable/text_selector" >  
  39.         </RadioButton>  
  40.   
  41.         <RadioButton  
  42.             android:id="@+id/tang"  
  43.             android:layout_width="wrap_content"  
  44.             android:layout_height="wrap_content"  
  45.              android:layout_marginLeft="-15dip"  
  46.             android:button="@null"  
  47.             android:drawableRight="@drawable/line"  
  48.             android:drawablePadding="10dip"  
  49.             android:text="汤"   
  50.             android:textColor="@drawable/text_selector">  
  51.         </RadioButton>  
  52.           
  53.         <RadioButton  
  54.             android:id="@+id/zhushi"  
  55.             android:layout_width="wrap_content"  
  56.             android:layout_height="wrap_content"  
  57.              android:layout_marginLeft="-15dip"  
  58.             android:button="@null"  
  59.             android:drawableRight="@drawable/line"  
  60.             android:drawablePadding="10dip"  
  61.             android:text="主   食"  
  62.             android:textColor="@drawable/text_selector" >  
  63.         </RadioButton>  
  64.           
  65.         <RadioButton  
  66.             android:id="@+id/zhou"  
  67.             android:layout_width="wrap_content"  
  68.             android:layout_height="wrap_content"  
  69.              android:layout_marginLeft="-15dip"  
  70.             android:button="@null"  
  71.             android:drawableRight="@drawable/line"  
  72.             android:drawablePadding="15dip"  
  73.             android:text="粥"  
  74.             android:textColor="@drawable/text_selector" >  
  75.         </RadioButton>  
  76.     </RadioGroup>  
  77.   
  78. </LinearLayout>  

text_selector.xml如下:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <selector xmlns:android="http://schemas.android.com/apk/res/android" >  
  3.       <item android:color="#ffffff" android:state_checked="true"/>  
  4.       <item android:color="#000000"/>  
  5. </selector>  
  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值