Android开发之RadioGroup与RadioButton控件使用

      RadioButton即单选按钮,它在开发中提供了一种“多选一”的操作模式,是Android开发中常用的一种组件,例如在用户注册时,选择性别时只能从“男”或者“女”中选择一个。与Web开发不同的是,在Android中可以使用RadioGroup来定义单选按钮组件。

     RadioGroup类的定义如下图所示:

   java.lang.Object
     android.view.View
         android.view.ViewGroup
             android.widget.LinearLayout
                 android.widget.RadioGroup

   RadioGroup提供的只是RadioButton单选按钮的容器,我们可以在该容器中添加多个RadioButton方可使用,要设置单选按钮的内容,则需要使用RadioButton类。

   RadioButton类的定义如下图所示:
  java.lang.Object
    android.view.View
        android.widget.TextView
            android.widget.Button
                android.widget.CompoundButton
                    android.widget.RadioButton
可以发现RadioButton类是Button类的子类,因此该组件与Button按钮组件的使用类似,区别在于定义的RadioButton组件必须放在RadioGroup组件中。

-----------------------------------------------------------------------------------------------------


RadioGroup的公共方法

public void addView (View child, int index, ViewGroup.LayoutParams params)

      使用指定的布局参数添加一个子视图

参数

                 child         所要添加的子视图

index         将要添加子视图的位置

params    所要添加的子视图的布局参数

                           

public void check (int id) 

        如果传递-1作为指定的选择标识符来清除单选按钮组的勾选状态,相当于调用clearCheck()操作

  参数

                id     该组中所要勾选的单选按钮的唯一标识符(id

              参见

                getCheckedRadioButtonId()

                clearCheck()

 

public void clearCheck () 

清除当前的选择状态,当选择状态被清除,则单选按钮组里面的所有单选按钮将取消勾选状态,getCheckedRadioButtonId()将返回null

              参见

                check(int)

                getCheckedRadioButtonId()

 

public RadioGroup.LayoutParams generateLayoutParams (AttributeSet attrs)   

基于提供的属性集合返回一个新的布局参数集合

参数

             attrs                   用于生成布局参数的属性

           返回值

             返回一个ViewGroup.LayoutParams或其子类的实例

 

public int getCheckedRadioButtonId ()  

返回该单选按钮组中所选择的单选按钮的标识ID,如果没有勾选则返回-1

              返回值

                返回该单选按钮组中所选择的单选按钮的标识ID

              参见

                check(int)

   clearCheck()

 

public void setOnCheckedChangeListener (RadioGroup.OnCheckedChangeListener listener)

注册一个当该单选按钮组中的单选按钮勾选状态发生改变时所要调用的回调函数

参数

             listener    当单选按钮勾选状态发生改变时所要调用的回调函数

                           

public void setOnHierarchyChangeListener (ViewGroup.OnHierarchyChangeListener listener)

注册一个当子内容添加到该视图或者从该视图中移除时所要调用的回调函数

参数

                            listener    当层次结构发生改变时所要调用的回调函数

 

  受保护方法

protected LinearLayout.LayoutParams generateDefaultLayoutParams ()

当布局为垂直方向时,将返回一个宽度为“填充父元素”(MATCH_PARENT),高度为“包裹内容”的布局参数集合,如果为水平方向时,将返回宽度为“包裹内容”,高度为“填充父元素”的布局参数集合

(match_parent即为fill_parent,public static final int FILL_PARENT/MATCH_PARENT = -1 )

        返回值

返回一个默认的布局参数集合

 

protected void onFinishInflate ()

当视图从XML中加载,且相应的子视图被添加之后,调用该方法,

即使子类重写了该方法,应该确保去调用父类的方法(通常放在方法在第一句),这样才能完成相应的调用参数

       返回值

返回一个默认的布局参数集合

 
------------------------------------------------------------------------------------------------------

   Layout布局文件代码如下:
 
        
        
在RadioGroup中使用
   android:orientation="horizontal"
来设定内部RadioButton的排列方式,horizontal为水平,vertical为垂直。
   android:checkedButton="@+id/male"
设置RadioGroup内默认选中的RadioButton。
   运行效果如下:


   单选按钮RadioGroup上我们可以进行事件处理操作,当用户选中某个选项之后将触发相应的监听器进行处理,该注册事件监听为OnCheckedChangeListener,具体方法为:
public void setOnCheckedChangeListener(RadioGroup.OnCheckedChangeListener listener)

  事件代码如下:

        /**RadioGroup与RadioButton**/
        sexRadioGroup = (RadioGroup)findViewById(R.id.rgSex);
        male = (RadioButton)findViewById(R.id.male);
        female = (RadioButton)findViewById(R.id.female);
        sexRadioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String msg = "";
                if(male.getId()==checkedId){
                    msg = "当前选中的性别为:"+male.getText().toString();
                }
                if(female.getId()==checkedId){
                    msg = "当前选中的性别为:"+female.getText().toString();
                }
                Toast.makeText(getApplicationContext(),msg,Toast.LENGTH_LONG).show();
            }
        });

效果如下图所示:

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

如果觉得对您有帮助,微信扫一扫支持一下:


你可能感兴趣的:(Android开发,Android应用开发)