当前位置: 首页 > web >正文

RadioGroup和RadioButton,单选框(Android)

转载自:https://www.cnblogs.com/Im-Victor/p/6238437.html
仅仅是为了以后查找方便,又怕博主删掉原文。望见谅。
实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个。并用setOnCheckedChangeListener来对单选按钮进行监听

RadioGroup相关属性:
RadioGroup.getCheckedRadioButtonId ();--获取选中按钮的id
RadioGroup.clearCheck ();//---清除选中状态
RadioGroup.check (int id);//---通过参入选项id来设置该选项为选中状态如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作
setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener); //--一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数
addView (View child, int index, ViewGroup.LayoutParams params);//---使用指定的布局参数添加一个子视图
//参数 child 所要添加的子视图    index 将要添加子视图的位置  params 所要添加的子视图的布局参数
RadioButton.getText();//获取单选框的值
//此外,RadioButton的checked属性设置为true,代码里调用RadioButton的check(id)方法,不会触发onCheckedChanged事件

案例:

1.定义布局文件:

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><LinearLayoutandroid:orientation="vertical"android:layout_width="match_parent"android:layout_height="wrap_content"android:layout_marginRight="5dp" ><TextViewandroid:id="@+id/radiogroup_info_id"android:layout_width="228px"android:layout_height="wrap_content"android:text="我选择的是...?"android:textSize="30sp"   />  <RadioGroupandroid:id="@+id/radioGroup_sex_id"android:layout_width="match_parent"android:layout_height="match_parent"><RadioButtonandroid:id="@+id/boy_id"android:layout_width="match_parent"android:layout_height="match_parent"  android:text="Boy"/><RadioButtonandroid:id="@+id/girl_id"android:layout_width="match_parent"android:layout_height="match_parent"  android:text="Girl"/></RadioGroup><Buttonandroid:id="@+id/radio_clear"android:layout_width="match_parent"android:layout_height="match_parent"  android:text="清除选中按钮"/><Buttonandroid:id="@+id/radio_add_child"android:layout_width="match_parent"android:layout_height="match_parent"  android:text="添加单选项"/></LinearLayout></ScrollView>

2.Java代码文件

package com.dream.app.start.first.radiobutton;
import com.dream.app.start.R;
import com.dream.app.start.R.id;
import com.dream.app.start.R.layout;
import com.dream.app.start.three.utils.PublicClass;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup.LayoutParams;
import android.widget.Button;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;
import android.widget.ToggleButton;
public class RadioButtonDemo extends PublicClass {private  TextView   textView=null;private  RadioGroup  radioGroup=null;private  RadioButton  radioButton_boy,radioButton_girl;private Button   radio_clear,child;/* (non-Javadoc)* <a href="http://my.oschina.net/u/244147" target="_blank" rel="nofollow">@see</a>  * android.app.Activity#onCreate(android.os.Bundle)*/@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.layout_frist_radiobuton);textView = (TextView)findViewById(R.id.radiogroup_info_id); //radioGroupradioGroup=(RadioGroup)findViewById(R.id.radioGroup_sex_id);radioButton_boy=(RadioButton)findViewById(R.id.boy_id);radioButton_girl=(RadioButton)findViewById(R.id.girl_id);child=(Button)findViewById(R.id.radio_add_child);//---radioGroup.setOnCheckedChangeListener(listen);radio_clear=(Button)findViewById(R.id.radio_clear);radio_clear.setOnClickListener(onClick);child.setOnClickListener(onClick);}private OnCheckedChangeListener  listen=new OnCheckedChangeListener() {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {int id= group.getCheckedRadioButtonId();switch (group.getCheckedRadioButtonId()) {case R.id.girl_id:textView.setText("我选择的是:"+radioButton_girl.getText());break;case R.id.boy_id:textView.setText("我选择的是:"+radioButton_boy.getText());break;default:textView.setText("我选择的是:新增");break;}}};private OnClickListener  onClick=new OnClickListener() {@Overridepublic void onClick(View v) {radio_clear=(Button)v;switch (radio_clear.getId()) {case R.id.radio_clear:radioGroup.check(-1);//清除选项
//              radioGroup.clearCheck(); //清除选项textView.setText("我选择的是...?");break;case R.id.radio_add_child://新增子RadioButton  newRadio =new RadioButton(getApplicationContext());newRadio.setText("新增");radioGroup.addView(newRadio, radioGroup.getChildCount());              break;//default:break;}}};}

运行示范 如图:
这里写图片描述
RadioButton和RadioGroup的关系:

1、RadioButton表示单个圆形单选框,而RadioGroup是可以容纳多个RadioButton的容器

2、每个RadioGroup中的RadioButton同时只能有一个被选中

3、不同的RadioGroup中的RadioButton互不相干,即如果组A中有一个选中了,组B中依然可以有一个被选中

4、大部分场合下,一个RadioGroup中至少有2个RadioButton

5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置

RadioButton和CheckBox的区别:

1、单个RadioButton在选中后,通过点击无法变为未选中

单个CheckBox在选中后,通过点击可以变为未选中

2、一组RadioButton,只能同时选中一个

 一组CheckBox,能同时选中多个

3、RadioButton在大部分UI框架中默认都以圆形表示

 CheckBox在大部分UI框架中默认都以矩形表示

☆定制RadioButton样式

RadioButton长成什么样子是由其Background、Button等属性决定的,Android系统
使用style定义了默认的属性,在android源码

android/frameworks/base/core/res/res/values/styles.xml中可以看到默认的定义:

1 <style name="Widget.CompoundButton.RadioButton"> 
2         <item name="android:background">@android:drawable/btn_radio_label_background</item> 
3         <item name="android:button">@android:drawable/btn_radio</item> 
4 </style>

即其背景图是btn_radio_label_background,其button的样子是btn_radio

btn_radio_label_background是什么?
其路径是android/frameworks/base/core/res/res/drawable-mdpi/btn_radio_label_background.9.png
可以看到是一个NinePatch图片,用来做背景,可以拉伸填充。

btn_radio是什么?

其路径是android/frameworks/base/core/res/res/drawable/btn_radio.xml

是个xml定义的drawable,打开看其内容:

<selector xmlns:android="http://schemas.android.com/apk/res/android">     <item android:state_checked="true" android:state_window_focused="false" android:drawable="@drawable/btn_radio_on" /> <item android:state_checked="false" android:state_window_focused="false" android:drawable="@drawable/btn_radio_off" /> <item android:state_checked="true" android:state_pressed="true" android:drawable="@drawable/btn_radio_on_pressed" /> <item android:state_checked="false" android:state_pressed="true" android:drawable="@drawable/btn_radio_off_pressed" /> <item android:state_checked="true" android:state_focused="true"           android:drawable="@drawable/btn_radio_on_selected" /> <item android:state_checked="false" android:state_focused="true" android:drawable="@drawable/btn_radio_off_selected" /> <item android:state_checked="false" android:drawable="@drawable/btn_radio_off" /> <item android:state_checked="true" android:drawable="@drawable/btn_radio_on" /> 
</selector>

自定义有三种方式:

1.方式一:

<?xml version="1.0" encoding="utf-8"?>    
<selector xmlns:android="http://schemas.android.com/apk/res/android">  
<!-- 未选中->  <item    android:state_checked="false"    android:drawable="@drawable/tabswitcher_long" /> 
<!--选中->    <item    android:state_checked="true"    android:drawable="@drawable/tabswitcher_short" />    
</selector>

在布局文件中使用

<RadioGroup ... 
> 
<RadioButton ... 
android:button="@null" 
android:background="@drawable/radio" 
/> 
</RadioGroup>

android:button="@null" 去除RadioButton前面的圆点
2.方式二:在JAVA代码中定义

@Override  
public boolean onTouchEvent(MotionEvent event) { if(event.getActionMasked() == MotionEvent.ACTION_DOWN){ this.setBackgroundResource(com.wxg.tab.R.drawable.main_bg); }else if(event.getActionMasked()== MotionEvent.ACTION_DOWN) { this.setBackgroundResource(com.wxg.tab.R.drawable.hui); } return super.onTouchEvent(event); 
}

去除RadioButton前面的圆点adioButton.setButtonDrawable(android.R.color.transparent);

  1. 方式三

使用XML文件定义,在JAVA代码中使用 radioButton.setBackgroundResource(R.drawable.radio);调用

==============================================================

                                            设置RadioButton在文字的右边
<b><RadioButton  android:id="@+id/button2"  android:layout_width="fill_parent"  android:layout_height="50dip"  android:button="@null"  android:drawableRight="@android:drawable/btn_radio"  //在右边android:paddingLeft="30dip"  android:text="Android高手"  android:textSize="20dip" />  

动态添加radiobutton进入radiogroup中。

 RadioGroup radioGroup=findViewById(R.id.activity_group);radioGroup.setOrientation(LinearLayout.HORIZONTAL);for (int i = 0; i < 10; i++) {RadioButton radioButton=new RadioButton(this);radioButton.setText(""+i);   //radiobutton的文字radioButton.setTextSize(12);radioButton.setTextColor(getResources().getColor(R.color.blue));radioButton.setCompoundDrawablePadding(40);    //貌似没什么用radioButton.setPadding(20,0,20,0);//radiobutton之间的间距radioButton.setCompoundDrawablePadding(20);radioGroup.addView(radioButton);}

如果一行的radiobutton过多,需要左右滑动,则布局文件中可以在radiogroup外层嵌套一个scrollview

 <HorizontalScrollViewandroid:layout_width="match_parent"android:layout_height="wrap_content"><RadioGroupandroid:id="@+id/activity_group"android:layout_width="match_parent"android:layout_height="wrap_content"/></HorizontalScrollView>
http://www.xdnf.cn/news/11458.html

相关文章:

  • 黑客必备的10款黑客武器(黑客工具合集)
  • 4438的代码分析一
  • 最完整的国内手机号段
  • 如何理解FFT中时间窗与RBW的关系
  • tf坐标系转换(gmapping)
  • 【网络安全-键盘监视】学会以后去捉弄舍友,看他有了什么不可告人的秘密
  • Android-Tangram模型:淘宝、天猫都在用的UI框架模型你一定要懂!
  • mysql数据库下载、安装、使用
  • 10个方法教你解决虚幻4运行崩溃问题
  • AsyncTask的使用和工作原理
  • estore简版商城思路
  • 深入理解java.lang.InstantiationError异常
  • JNI常用数据类型转换库函数使用总结
  • 分享我免费可用API接口网站
  • bootstrap方法_【统计学】bootstrap方法
  • 软件测试方法_边界值分析法
  • 地图上分成一块一块区域 高德地图_开车用哪个导航最好?看看老司机总结的地图对比,学会不吃亏...
  • Couchbase数据备份与恢复
  • CountDownTimer 倒计时,定时器工具类
  • WTL简介
  • C#对ListBox控件中的数据进行的操作
  • JVM内存配置详
  • 高级分布式系统-第11讲 现场总线技术
  • 杭电oj--人见人爱A+B 2033
  • 线性回归(Linear Regression)
  • 一个外国的好网站 http://www.ilovejackdaniels.com/
  • Serialization即序列化全解析(转)
  • 无线网络领域国际会议/期刊排名
  • CSS教程:div垂直居中的N种方法
  • SLIC超像素分割详解(一)(二)(三)