RadioGroup和RadioButton

RadioButton,就是一个单选按钮,Radioroup包含一组RadioButton,供选择,这里只能单选,并且选中后再次点击不会取消选中。

 

常用属性:

android:checked="true"                                   设置 是否为选中状态  

android:text="@string/title1_button2"           设置单选的文字内容

android:layout_marginLeft="30dp"               控制两个单选按钮之间的距离

android:paddingLeft="20dp"                          控制单选按钮和文字之间的距离

android:button="@drawable/radio"              设置单选按钮的点击效果

 

布局文件如下:

[java]  view plain copy
  1. <RadioGroup   
  2.         android:id="@+id/title1_group"   
  3.         android:layout_width="wrap_content"   
  4.         android:layout_height="wrap_content"   
  5.         android:orientation="horizontal"  
  6.         android:layout_marginLeft="10dp">   
  7.          <RadioButton   
  8.              android:id="@+id/title1_button1"   
  9.              android:layout_width="wrap_content"   
  10.              android:layout_height="wrap_content"   
  11.              android:text="@string/title1_button1"    
  12.              android:textColor="@color/description_text_color"    
  13.              android:button="@drawable/radio"    
  14.              android:paddingLeft="20dp"     
  15.              android:textSize="@dimen/title_text_size"              
  16.          />    
  17.           <RadioButton   
  18.              android:id="@+id/title1_button2"   
  19.              android:layout_width="wrap_content"   
  20.              android:layout_height="wrap_content"   
  21.              android:text="@string/title1_button2"    
  22.              android:textColor="@color/description_text_color"    
  23.              android:textSize="@dimen/title_text_size"  
  24.              android:button="@drawable/radio"     
  25.              android:paddingLeft="20dp"  
  26.              android:layout_marginLeft="30dp" 设置和上一个单选按钮的左边距离30dp  
  27.          />    
  28.         </RadioGroup>     


在代码中添加方式如下:

[java]  view plain copy
  1.               
  2.                 RadioButton radioBtn1, radioBtn2;  
  3.         
  4.                 LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(  
  5.         LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT);  
  6.   
  7. params.gravity = Gravity.CENTER_VERTICAL;  
  8.   
  9.                 RadioGroup radioGroup = new RadioGroup(this);  
  10.         radioGroup.setLayoutParams(params);  
  11. radioGroup.setOrientation(RadioGroup.HORIZONTAL);  
  12.   
  13. radioBtn1 = new RadioButton(this);  
  14. // 界面布局宽度,高度  
  15. LinearLayout.LayoutParams lp1 = new LinearLayout.LayoutParams(  
  16.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  17. radioBtn1.setLayoutParams(lp1);  
  18. radioBtn1.setText("按钮1");  
  19.         radioBtn1.setTextColor(Color.parseColor("#999d9c"));  
  20.   
  21. radioGroup.addView(radioBtn1);  
  22.   
  23. radioBtn2 = new RadioButton(this);  
  24. LinearLayout.LayoutParams lp2= new LinearLayout.LayoutParams(             
  25.         LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);  
  26. radioBtn2.setLayoutParams(lp2);  
  27.                 radioBtn2.setTextColor(Color.parseColor("#999d9c"));        radioBtn2.setText("按钮2");                   radioGroup.addView(radioBtn2);  


设置RadioGroup的监听

[java]  view plain copy
  1.                title1_group = (RadioGroup) findViewById(R.id.title1_group);  
  2. itle1_group.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  3.   
  4. @Override  
  5. public void onCheckedChanged(RadioGroup group, int checkedId) {  
  6.     title1_group_isSelected = true;  
  7.     if (checkedId == R.id.title1_button1) {  
  8.         title1_group_selected = 0;  
  9.   
  10.     } else if (checkedId == R.id.title1_button2) {  
  11.         title1_group_selected = 1;  
  12.     }  
  13. }  
  14. );  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值