android 控件之RadioGroup&RadioButton

本文介绍了Android中RadioGroup和RadioButton的使用。RadioGroup作为RadioButton的容器,保证同一组内只有一个单选框被选中。讲解了如何设置初始选中状态、使用setOnCheckedChangeListener进行监听,以及XML布局中的配置,包括使文字在前单选框在后的实现。还对比了RadioButton与CheckBox的区别,如选中状态切换、选中数量限制和默认形状等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

RadioButton和RadioGroup的关系:

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

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

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

    4、一个RadioGroup中至少有2个RadioButton;

    5、大部分场合下,一个RadioGroup中的RadioButton默认会有一个被选中,并建议您将它放在RadioGroup中的起始位置(默认选中方法android:checked="true" );

      我们在代码中使用  setOnCheckedChangeListener 来对单选按钮进行监听。


代码示例:

XML文件:

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    > 
    <TextView    
        android:layout_width="fill_parent"   
        android:layout_height="wrap_content"   
        android:text="@string/hello" />

   <RadioGroup 
        android:id="@+id/radioGroup" 
        android:layout_width="wrap_content" 
        android:layout_height="wrap_content" 
        android:orientation="vertical">

        <RadioButton 
             android:id="@+id/radioBtn1" 
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content" 
             android:text="@string/radioBtn1" 
             android:textColor="#ffffff" /> 
 
        <RadioButton 
             android:id="@+id/radioBtn2" 
             android:layout_width="wrap_content" 
             android:layout_height="wrap_content" 
             android:text="@string/radioBtn2"  /> 
     </RadioGroup> 
</LinearLayout> </span>

布局格式如图:


若是希望,文字在前单选框在后只要在main.xml布局文件中的<RadioButton/>加入

android:button="@null"

android:drawableRight="@android:drawable/btn_radio"即可

效果及如下图显示:


string.xml

<span style="font-size:12px;"><?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="app_name">单选按钮测试</string>
    <string name="hello">你的性别是</string>
    <string name="radioBtn1">男</string>
    <string name="radioBtn2">女</string>
</resources> </span>

MainActivity.java

<span style="font-size:12px;">package com.android.radiobutton;  

import android.app.Activity;  
import android.os.Bundle;  
import android.widget.RadioGroup;  
import android.widget.Toast;  
     
public class MainActivity extends Activity {  
          
    //声明RadioGroup  
    RadioGroup raGroup;  
    @Override 
    public void onCreate(Bundle savedInstanceState) {  
       super.onCreate(savedInstanceState);  
       setContentView(R.layout.main);  
              
       //通过findViewById获得RadioGroup对象  
       raGroup=(RadioGroup)findViewById(R.id.radioGroup);  

       // 添加事件监听器     
       raGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {  
                  
           @Override 
           public void onCheckedChanged(RadioGroup group, int checkedId) {  
               // TODO Auto-generated method stub  
               if(checkedId==R.id.radioBtn1){                    
                  Toast.makeText(MainActivity.this, "你的性别是男", Toast.LENGTH_LONG).show();  
               }</span><pre name="code" class="java"><span style="font-size:12px;">               else { 
                   Toast.makeText(MainActivity.this, "你的性别是女", Toast.LENGTH_LONG).show();  
               }  
           }  
        });         
    }  
} </span>

 

延伸:RadioButton和CheckBox的区别:

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

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

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

         一组CheckBox,能同时选中多个

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

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

具体代码示例:

main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

   <TextView
       android:layout_width="fill_parent"
       android:layout_height="wrap_content"
       android:text="@string/hello"
       android:textSize="20sp"
       android:textStyle="bold"
       android:textColor="#FFFFFF" />
    
   <CheckBox
      android:id="@+id/checkbox1"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="@string/football"
      android:textSize="16sp" />
    
   <CheckBox
       android:id="@+id/checkbox2"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/basketball"
       android:textSize="16sp" />
    
   <CheckBox
       android:id="@+id/checkbox3"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:text="@string/volleyball"
       android:textSize="16sp"/>
</LinearLayout> 

strings.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <string name="hello">你喜欢的运动是</string>
    <string name="app_name">复选按钮测试</string>
    <string name="football">足球</string>
    <string name="basketball">篮球</string>
    <string name="volleyball">排球</string>
</resources>

MainActivity.java

    package com.android.checkbox;  
     
    import android.app.Activity;  
    import android.os.Bundle;  
    import android.widget.CheckBox;  
    import android.widget.CompoundButton;  
    import android.widget.Toast;  
    import android.widget.CompoundButton.OnCheckedChangeListener;  
     
    public class MainActivity extends Activity{  
        //声明复选按钮  
        private CheckBox cBox1;  
        private CheckBox cBox2;  
        private CheckBox cBox3;  
          
        @Override 
        public void onCreate(Bundle savedInstanceState){  
            super.onCreate(savedInstanceState);  
            setContentView(R.layout.main);  
            //通过findViewById获得CheckBox对象  
            cBox1=(CheckBox)findViewById(R.id.checkbox1);  
            cBox2=(CheckBox)findViewById(R.id.checkbox2);  
            cBox3=(CheckBox)findViewById(R.id.checkbox3);  
     
            //注册事件监听器  
            cBox1.setOnCheckedChangeListener(listener);  
            cBox2.setOnCheckedChangeListener(listener);  
            cBox3.setOnCheckedChangeListener(listener);  
     
        }  
        //响应事件  
        private OnCheckedChangeListener listener = new OnCheckedChangeListener(){  
            @Override 
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked)  
            {  
                //cBox1被选中  
                if (buttonView.getId()==R.id.checkbox1){  
                    if (isChecked){  
                        Toast.makeText(MainActivity.this, "你喜欢足球", Toast.LENGTH_LONG).show();  
                    }  
                }  
                //cBox2被选中  
                else if (buttonView.getId()==R.id.checkbox2){  
                    if (isChecked){  
                        Toast.makeText(MainActivity.this, "你喜欢篮球", Toast.LENGTH_LONG).show();  
                    }  
                }  
                //cBox3被选中  
                else if (buttonView.getId()==R.id.checkbox3){  
                    if (isChecked){  
                        Toast.makeText(MainActivity.this, "你喜欢排球", Toast.LENGTH_LONG).show();  
                    }  
                }         
            }  
        };  
    } 

效果图如下:



到此结束~~~




评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值