您好!欢迎来到源码码网

Android单选按钮控件RadioGroup使用

  • 源码教程
  • 来源:源码码网
  • 编辑:admin
  • 时间:2021-01-08 19:11
  • 阅读:783

RadioGroup 为单项选择按钮组,其中可以包含多个 RadioButton,即单选按钮,它们共同为用户提供一种多选一的选择方式。

在多个 RadioButton 被同一个 RadioGroup 包含的情况下,多个 RadioButton 之间自动形成互斥关系,仅有一个可以被选择。

单选按钮的使用方法和 CheckBox 的使用方法高度相似,其事件监听接口使用的是 RadioGroup.OnCheckedChangeListener(),使用 setOnCheckedChangeListener() 方法将监听器设置到单选按钮上。

按照 CheckBox 的讲解思路,启动一个名为 RadioGroupActivity 的 Activity 来对 RadioGroup 进行讲解。

RadioGroupActivity 的运行效果如图 1 所示。

RadioGroup的应用界面

图 1  RadioGroup 的应用界面

在工程 WidgetDemo 的布局文件 main.xml 中添加一个 Button,并启动 RadioGroupActivity 的相关代码。

在 main.xml 中添加代码如下:

  1. <Button

  2. android:id="@+id/button3"

  3. android:layout_width="wrap_content"

  4. android:layout_height="wrap_content"

  5. android:text="RadioGroupDemo"/>

启动处理 RadioGroup 的 Activity RadioGroupActivity 的代码如下:

  1. Button radiotn = (Button)this.findViewById(R.id.button3);

  2. radiotn.setOnClickListener(new OnClickListener(){


  3. @Override

  4. public void onClick(View v){

  5.     Intent intent = new Intent(WidgetDemoActivity.this,RadioGroupActivity.class);

  6.     startActivity(intent);

  7.   }


  8. })

同时在 AndroidManifest.xml文件中声明该 Activity:

<activity android:name=".RadioGroupActivity "></activity>

RadioGroupActivity 使用的是 radiogroup.xml,其代码如下:


  1. <?xml version="1.0" encoding="utf-8"?>


  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"

  3. android:layout_width="match_parent"

  4. android:layout_height="match_parent"

  5. android:orientation="vertical">


  6. <TextView

  7. android:id="@+id/radiohello"

  8. android:layout_width="fill_parent"

  9. android:layout_height="wrap_content"

  10. android:text="@string/hello" />


  11. <RadioGroup

  12. android:id="@+id/radiogroup1"

  13. android:layout_width="wrap_content"

  14. android:layout_height="wrap_content"

  15. android:layout_x="3px"

  16. android:orientation="vertical">


  17. <RadioButton

  18. android:id="@+id/radiobutton1"

  19. android:layout_width="wrap_content"

  20. android:layout_height="wrap_content"

  21. android:text="@string/music" />


  22. <RadioButton

  23. android:id="@+id/radiobutton2"

  24. android:layout_width="wrap_content"

  25. android:layout_height="wrap_content"

  26. android:text="@string/gym" />


  27. <RadioButton

  28. android:id="@+id/radiobutton3"

  29. android:layout_width="wrap_content"

  30. android:layout_height="wrap_content"

  31. android:text="@string/dance" />


  32. <RadioButton

  33. android:id="@+id/radiobutton4"

  34. android:layout_width="wrap_content"

  35. android:layout_height="wrap_content"

  36. android:text="@string/lookBook" />


  37. </RadioGroup>

  38. </LinearLayout>

该布局文件使用了 LinearLayout 布局,并且在其中放置了一个 TextView 和一个 RadioGroup。RadioGroup 中含有三个 RadioButton。这些组件对应的 strings.xml 文件中定义的变量为:

  1. <resources>

  2. <string name="radiohello">选择你最喜欢的课程:</string>

  3. <string name="music">音乐</string>

  4. <string name="gym">体育</string>

  5. <string name="dance">舞蹈</string>

  6. <string name="lookBook">看书</string>

  7. </resources>

RadioGroupActivity.java 的代码如下:


package introduction.android.widgetdemo;

import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;


public class RadioGroupActivity extends Activity {
    private TextView textView;
    private RadioGroup radiogroup;
    private RadioButton radio1,radio2,radio3,radio4;

    @Override
    protected void onCreate(Bundle saveInstanceState) {
        super.onCreate(saveInstanceState);
        this.setContentView(R.layout.radiogroup);
        textView = (TextView) findViewById(R.id.radiohello);
        radiogroup = (RadioGroup)findViewById(R.id.radiogroup1);
        radio1 = (RadioButton) findViewById(R.id.radiobutton1);
        radio2 = (RadioButton) findViewById(R.id.radiobutton2);
        radio3 = (RadioButton) findViewById(R.id.radiobutton3);
        radio4 = (RadioButton) findViewById(R.id.radiobutton4);
        radiogroup.setOnCheckedChangeListener(new RadioGroup.
                                  OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                String text="我最喜欢运动是";
                if (checkedId == radio1.getId()) {
                    text+=radio1.getText().toString();
                    textView.setText(text);
                } else if(checkedId == radio2.getId()){
                    text+=radio2.getText().toString();
                    textView.setText(text);
                }else if(checkedId == radio3.getId()) {
                    text += radio3.getText().toString();
                    textView.setText(text);
                }else if(checkedId == radio4.getId()) {
                    text += radio4.getText().toString();
                    textView.setText(text);
                }
            }
        });
    }
}

在 RadioGroupActivity 的 onCreate() 方法中为 RadioGroup 添加监视器 RadioGroup。

OnCheckedChangeListener 在其回调方法 onCheckedChanged() 中对 4 个 RadioButton 分别进行处理。需要说明的是,如果把 RadioGroup 去掉,只使用 RadioButton 的话,则需要为每个 RadioButton 单独设置监听器,其使用方法和 CheckBox 没有任何区别。

特别声明:
1、如无特殊说明,内容均为本站原创发布,转载请注明出处;
2、部分转载文章已注明出处,转载目的为学习和交流,如有侵犯,请联系客服删除;
3、编辑非《源码码网》的文章均由用户编辑发布,不代表本站立场,如涉及侵犯,请联系删除;
全部评论(0)
推荐阅读
  • 基于NetCore(Razor Page)开发的Cms建站系统MIT协议
  • 基于NetCore(Razor Page)开发的Cms建站系统MIT协议
  • FytSoaCms一款基于NetCore2.2/3.1(RazorPage)功能强大的Cms建站系统,支持前后端分离。简介:模块化:全新的架构和模块化的开发机制,便于灵活扩展和二次开发。模型/栏目/分类信息体系:通过栏目和模型绑定,以及不同的模型类型,不同栏目可以实现差异化的功能,轻松实现诸如资讯、下载、讨论和图片等功能。通过分类信息和栏目绑定,可以自动建立索引表,轻松实现复杂的信息检索。FytSoa是一套基于NetCore+SqlS
  • 开发工具
  • 来源:gitee
  • 编辑:源码码网
  • 时间:2026-01-24 22:21
  • 阅读:255
  • 基于JAVA开发的企业级平台微信公众号管理系统SmartWx
  • 基于JAVA开发的企业级平台微信公众号管理系统SmartWx
  • SmartWx微信公众号管理系统是一个完整的微信公众号web操作版,直接编译即可运行。让用户不再用关心微信的底层接口,直接使用页面进行操作,简单方便。包括服务器绑定、文本管理、图文管理、菜单管理、粉丝管理、群发消息等。技术框架开发语言:JAVA数据库:MYSQLJAVA开发框架:SpringMVC+Spring+Mybatis缓存框架:j2cache前端开发框架:Layui+JQuery+html前台模板引擎:art-templat
  • 源码教程
  • 来源:gitee
  • 编辑:源码码网
  • 时间:2026-01-24 21:53
  • 阅读:255
  • 前后端分离的Java快速开发平台renren-security可免费商用
  • 前后端分离的Java快速开发平台renren-security可免费商用
  • renren-security是一个轻量级的,前后端分离的Java快速开发平台,能快速开发项目并交付【接私活利器】采用SpringBoot3.x、Shiro、MyBatis-Plus、Vue3、TypeScript、ElementPlus、VueRouter、Pinia、Axios、Vite框架,开发的一套权限系统,极低门槛,拿来即用。设计之初,就非常注重安全性,为企业系统保驾护航,让一切都变得如此简单。提供了代码生成器,只
  • 源码教程
  • 来源:gitee
  • 编辑:源码码网
  • 时间:2026-01-23 13:03
  • 阅读:157
  • 离线IP地址定位库和IP定位数据管理框架Ip2region
  • 离线IP地址定位库和IP定位数据管理框架Ip2region
  • Ip2region是什么ip2region -是一个离线IP地址定位库和IP定位数据管理框架,同时支持IPv4和IPv6,10微秒级别的查询效率,提供了众多主流编程语言的 xdb 数据生成和查询客户端实现。Ip2region特性1、离线定位库项目本身同时了提供了一份IPv4(data/ipv4_source.txt)和IPv6(data/ipv6_source.txt)的原始数据和对应的xd
  • 源码教程
  • 来源:gitee
  • 编辑:源码码网
  • 时间:2026-01-22 11:26
  • 阅读:208
  • 常用测试压力工具使用介绍
  • 常用测试压力工具使用介绍
  • ab 是 ApacheBench 工具的缩写,它是一个HTTP压力测试工具。让我详细说明如何测试:1. 安装ApacheBenchWindows系统:方法一:安装XAMPP或WAMP(自带ab)下载地址:https://www.apachefriends.org/zh_cn/index.html安装后,ab工具在:C:xamppapacheinab.exe方法二:使
  • 开发工具
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2026-01-13 20:27
  • 阅读:143
联系客服
源码代售 源码咨询 技术开发 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部