您好!欢迎来到源码码网

Android单选按钮控件RadioGroup使用

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

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)
推荐阅读
  • js实现一键复制的几种方法
  • js实现一键复制的几种方法
  • JS实现一键复制可以使用以下方法:1、使用document.execCommand()方法实现复制:function copyTextToClipboard(text) {  var textarea = document.createElement(”textarea”);  textarea.value =
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-05-18 13:44
  • 阅读:147
  • bootstrap ui框架能用在uniapp中吗?
  • bootstrap ui框架能用在uniapp中吗?
  • BootstrapUI框架通常是前端开发中的一种工具,它提供了一套预定义的CSS样式和组件,用于快速构建响应式布局的网页。然而,UniApp是一个使用Vue.js开发跨平台应用的框架,它可以用来开发iOS、Android、以及各种小程序和H5应用。
  • 互动社区
  • 来源:源码码网
  • 编辑:热度建站
  • 时间:2024-04-12 00:04
  • 阅读:298
  • css实现banner图由中心点动态放大效果
  • css实现banner图由中心点动态放大效果
  • 在日常的网页设计中,为了让网页增加一定的特效以达到交互的目的,我们尝尝会在网页中使用一些动画效果。今天来说说实现banner图由中心点动态放大效果,实现这个效果需要用到css中的动画:animation​和关键帧:@keyframes,具体示例如下:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-11 18:52
  • 阅读:304
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • countUp.js实现鼠标滑动到某个位置数字自动滚动增加的效果
  • 在网页开发中为了提升网页的交互效果,经常会用到使用js给网页增加一定的特效,下边就来说说使用js实现鼠标滑动到某个位置数字自动滚动增加的效果。其实这种效果有很多中解决办法,自己也可以去写,下边我们借助countUp.js来实现,关于这个js文件,我放在末尾:
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-08 09:20
  • 阅读:399
  • 响应式网页设计思路及注意事项
  • 响应式网页设计思路及注意事项
  • 一、什么是响应式网页响应式网页设计就是让网页具有根据设备类型应用CSS样式的能力。设计:设想、计划。设计就是把想法实现。网页设计:按照一定的设计思路布局网页内容。传统网页设计:都是针对PC端浏览器而设计的,不具备查询设备的能力,更不能对多种访问设备做出响应。传统网页设计的弊端:在移动互联网时代,传统的网页设计不适合多屏幕时代。响应式网页设计应运而生。响应式网页设计是一种设计网页的思想/方法。响应:指让我们的网页能够自动查询用户的访问设备
  • 源码教程
  • 来源:源码码网
  • 编辑:源码码网
  • 时间:2024-04-02 11:24
  • 阅读:287
联系客服
源码代售 源码咨询 素材咨询 联系客服
029-84538663
手机版

扫一扫进手机版
返回顶部