您好!欢迎来到源码码网

Android单选按钮控件RadioGroup使用

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

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)
推荐阅读
  • 大型后台管理系统,用户登录状态该如何保存?
  • 大型后台管理系统,用户登录状态该如何保存?
  • 大型后台管理系统的用户登录状态保存需要综合考虑安全性、用户体验和系统架构。以下是企业级的完整方案:1.多层级存储策略class AuthManager {    constructor() {        this.storage = { &n
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2025-11-06 12:16
  • 阅读:350
  • 源码交易平台的支付困局与解决方案:如何通过专业支付系统提升交易效率
  • 源码交易平台的支付困局与解决方案:如何通过专业支付系统提升交易效率
  • 在数字经济蓬勃发展的今天,源码交易市场已成为互联网创业者和开发商的重要资源池。从电商系统源码到社交应用框架,从小程序解决方案到企业级管理系统,越来越多的开发者、初创企业和传统商家通过源码交易平台快速获取技术资产,实现商业目标的加速。源码交易市场的繁荣反映了数字化转型的迫切需求——企业需要快速迭代,开发者需要快速变现,用户需要快速启动。然而,在这个高速发展的市场中,一个长期被忽视但至关重要的问题浮现出来:支付系统的效率与安全性已成为制约交
  • 行业资讯
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2025-10-23 15:16
  • 阅读:298
  • Spring Boot 工程中 maven-surefire-plugin 测试执行失败及解决方法
  • Spring Boot 工程中 maven-surefire-plugin 测试执行失败及解决方法
  • 在SpringBoot工程编译时遇到maven-surefire-plugin的测试执行失败错误(Failedtoexecutegoalorg.apache.maven.plugins:maven-surefire-plugin:3.5.3:test),通常与测试环节相关。以下是常见原因及解决方法:1.测试用例执行失败• 原因:最常见的是测试用例(*Test.java)运行时抛出异常(如断言失败、空指针等),导
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2025-10-13 10:57
  • 阅读:363
  • WeMark - 微信小程序图片水印
  • WeMark - 微信小程序图片水印
  • 一个纯前端的微信小程序图片水印工具。支持文字/图片水印、单个与全屏两种模式,透明度与角度调节、单个水印位置X/Y控制,预览与对比模态、历史记录(100条)等功能。
  • 源码教程
  • 来源:源码码用户
  • 编辑:yg
  • 时间:2025-09-22 16:09
  • 阅读:287
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部