Android 控件开发之 RadioButton

本文详细介绍了Android平台下单选按钮RadioButton的使用方法及与RadioGroup的配合,包括基本概念、布局实现、事件监听等核心内容。通过示例代码展示了如何创建单选按钮并设置监听事件,实现用户交互。

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

单选按钮RadioButton在Android平台上也应用的非常多,比如一些选择项的时候,会用到单选按钮,实现单选按钮由两部分组成,也就是RadioButton和RadioGroup配合使用

RadioButton的单选按钮;

RadioGroup是单选组合框,用于将RadioButton框起来;

在没有RadioGroup的情况下,RadioButton可以全部都选中;

当多个RadioButton被RadioGroup包含的情况下,RadioButton只可以选择一个;

注意:单选按钮的事件监听用setOnCheckedChangeListener来对单选按钮进行监听


 RadioButton效果:

  

  

   本程序的main.xml源码:

   

[cpp]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <RadioGroup  
  8.     android:id="@+id/radioGroup"  
  9.     android:layout_width="wrap_content"    
  10.     android:layout_height="wrap_content"    
  11.     android:orientation="vertical">  
  12.       
  13. <RadioButton  
  14.     android:id="@+id/radioBlue"    
  15.     android:layout_width="fill_parent"   
  16.     android:layout_height="wrap_content"   
  17.     android:text="blue"/>  
  18.       
  19. <RadioButton  
  20.     android:id="@+id/radioRed"    
  21.     android:layout_width="fill_parent"   
  22.     android:layout_height="wrap_content"   
  23.     android:text="red"/>   
  24.       
  25. </RadioGroup>  
  26.   
  27. </LinearLayout>  



RadioButton事件响应setOnCheckedChangeListener

本程序的java源码:

[cpp]  view plain copy
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.widget.RadioGroup;  
  4. import android.widget.Toast;  
  5.   
  6.   
  7. public class RadioButtonActivity extends Activity   
  8. {  
  9.     /** Called when the activity is first created. */  
  10.   
  11.     @Override  
  12.     public void onCreate(Bundle savedInstanceState)   
  13.     {  
  14.     super.onCreate(savedInstanceState);  
  15.     setContentView(R.layout.main);  
  16.                 
  17.          final RadioGroup group = (RadioGroup)findViewById(R.id.radioGroup);     
  18.   
  19.          group.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()  
  20.          {          
  21.             @Override    
  22.             public void onCheckedChanged(RadioGroup group, int checkedId)  
  23.             {       
  24.         switch(checkedId)  
  25.         {  
  26.         case R.id.radioBlue:  
  27.             Toast.makeText(getApplicationContext(), "你选中了蓝色按钮", Toast.LENGTH_LONG).show();  
  28.             break;  
  29.         case R.id.radioRed:  
  30.             Toast.makeText(getApplicationContext(), "你选中了红色按钮", Toast.LENGTH_LONG).show();    
  31.             break;  
  32.         }  
  33.             }     
  34.         });     
  35.     }     
  36. }  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值