java radiogroup_Android基础控件RadioGroup使用方法详解

article/2025/9/22 6:08:41

本文为大家分享了Android基础控件RadioGroup的使用,供大家参考,具体内容如下

1.简单介绍

RadioGroup可以提供几个选项供用户选择,但只能选择其中的一个。其下面可以横着或者竖着挂几个RadioButton,也可以挂载其他控件(如TextView)。RadioGroup的相应事件一般不由下面的RadioButton响应,而是直接由RadioGroup响应。实现RadioGroup.OnCheckedChangeListener接口即可监听RadioGroup。RadioButton也是派生自CompoundButton,也可以通过修改button属性来修改图标,但是通过button属性修改往往会使文字和图标挨得很近。这时候我们可以设置RadioButton的drawableLeft属性和drawablePadding属性来使图标和文字挨得远一点(同时把button属性设置成@null)。下图是RadioGroup的使用效果。

13c2081c1cecba385d5cf14a41543c85.png

2.简单使用

下面是RadioGroup的简单实现代码。

radio_group_selector.xml

activity_radio_group.xml

xmlns:app="http://schemas.android.com/apk/res-auto"

xmlns:tools="http://schemas.android.com/tools"

android:layout_width="match_parent"

android:layout_height="match_parent"

tools:context=".RadioGroupActivity"

android:orientation="vertical">

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="18sp"

android:textColor="#000000"

android:text="这是横着放的RadioGroup"/>

android:id="@+id/rg_horizontal_demo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="horizontal">

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:checked="false"

android:text="好"

android:textSize="18sp"

android:id="@+id/rb_horizontal_good"

android:textColor="#000000"/>

android:layout_width="0dp"

android:layout_height="wrap_content"

android:layout_weight="1"

android:checked="false"

android:text="很好"

android:textSize="18sp"

android:id="@+id/rb_horizontal_very_good"

android:textColor="#000000"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="18sp"

android:textColor="#000000"

android:text="这是竖着放的RadioGroup"/>

android:id="@+id/rg_vertical_demo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="1"

android:checked="false"

android:text="好"

android:textSize="18sp"

android:id="@+id/rb_vertical_good"

android:textColor="#000000"/>

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="1"

android:checked="false"

android:text="很好"

android:textSize="18sp"

android:id="@+id/rb_vertical_very_good"

android:textColor="#000000"/>

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="18sp"

android:textColor="#000000"

android:text="这是改了图标竖着放的RadioGroup"/>

android:id="@+id/rg_vertical_custom_demo"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:orientation="vertical">

android:button="@drawable/radio_button_selector"

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="1"

android:checked="false"

android:text="这个是直接设置button的RadioButton"

android:textSize="18sp"

android:id="@+id/rb_vertical_custom_good"

android:textColor="#000000"/>

android:button="@null"

android:drawableLeft="@drawable/radio_button_selector"

android:drawablePadding="10dp"

android:layout_width="wrap_content"

android:layout_height="0dp"

android:layout_weight="1"

android:checked="false"

android:text="这个是设置drawableLeft属性的RadioButton"

android:textSize="18sp"

android:id="@+id/rb_vertical_custom_very_good"

android:textColor="#000000"/>

RadioGroupActivity.java

package xyz.strasae.androidlearn.my;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;

import android.widget.RadioButton;

import android.widget.RadioGroup;

import android.widget.Toast;

public class RadioGroupActivity extends AppCompatActivity {

RadioGroup rg_horizontal_demo;

RadioGroup rg_vertical_demo;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_radio_group);

rg_horizontal_demo = findViewById(R.id.rg_horizontal_demo);

rg_vertical_demo = findViewById(R.id.rg_vertical_demo);

rg_horizontal_demo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup radioGroup, int i) {

RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());

Toast.makeText(RadioGroupActivity.this, String.format("你选择了%s", rb_temp.getText().toString()), Toast.LENGTH_SHORT).show();

}

});

rg_vertical_demo.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {

@Override

public void onCheckedChanged(RadioGroup radioGroup, int i) {

RadioButton rb_temp = findViewById(radioGroup.getCheckedRadioButtonId());

Toast.makeText(RadioGroupActivity.this, String.format("你选择了%s", rb_temp.getText().toString()), Toast.LENGTH_SHORT).show();

}

});

}

}

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。


http://chatgpt.dhexx.cn/article/upC4oRK3.shtml

相关文章

android自定义radiogroup,Android自定义RadioGroup

最近做项目时需要用到RadioGroup,发现Android原生的RadioGroup太丑了,所以自己写了一个,效果如下所示: 其实就是由4个Button组成的LinearLayout,只是为了方便点击效果的切换所以封装了一下。代码如下: pack…

RadioGroup

实现RadioButton由两部分组成,也就是RadioButton和RadioGroup配合使用.RadioGroup是单选组合框,可以容纳多个RadioButton的容器.在没有RadioGroup的情况下,RadioButton可以全部都选中;当多个RadioButton被RadioGroup包含的情况下,…

Android入门之路 - RadioGroup、RadioButton、CheckBox(单复选框)使用进阶

本文只为初级的Android新手而写,多掌握一份简单实用的技能,快速get吧,有问题就留言 2022:蓦然回首,已入行多年,人生的第二个迷茫阶段 初级 - 使用方式RadioGroup RadioButtonCheckBoxDemo示例 CheckBox 自…

Android RadioGroup 单选按钮控件

Android RadioGroup 单选按钮控件 RadioGroup 为单项选择按钮组,其中可以包含多个 RadioButton,即单选按钮,它们共同为用户提供一种多选一的选择方式。在多个 RadioButton 被同一个 RadioGroup 包含的情况下,多个 RadioButton 之间…

RadioGroup控件使用

在只能进行单选的选择上面可以通过&#xff32;adioGroup控件来实现&#xff0c;例如性别选择以及考试的单项选择题。 xml布局如下&#xff1a; <?xml version"1.0" encoding"utf-8"?> <androidx.constraintlayout.widget.ConstraintLayout xm…

RedHat9.0下载地址

RedHat下载&#xff1a;http://archive.download.redhat.com/pub/redhat/linux/9/en/iso/i386/ 转载于:https://www.cnblogs.com/XACOOL/p/5679613.html

下载redhat4.8的方法

一、背景 因为老软件需要安装&#xff0c;所以找个了老系统来安装。 二、上官网 https://www.redhat.com/zh/ 点开redhat最新版本&#xff0c;现在是8.0 下载 要求我登录账户&#xff0c;我就登录jhui163的 然后&#xff0c;看到7.0和更早期的超链接&#xff0c;点进去

vmware安装redhat 8

vmware安装redhat 8 1、下载镜像文件1.1 镜像文件 2、安装系统2.1、选择自定义安装2.2、兼容性选择2.3、选择镜像文件导入2.4、设置用户名密码2.5、选择虚拟机在磁盘上的位置2.6、选择处理器数量2.7、选择内存大小2.8、选择桥接或NAT2.9、选择SCSI控制器类型2.10、选择虚拟机磁…

RedHat 7.5 7.6下载磁力链分享

某度最近更新了一波&#xff0c;导致诸多屏蔽弹客户端应用直接显示直链下载的浏览器插件也失效&#xff0c;就连最强的下载神器也400、403报错 csdn的下载站里是有不少资源&#xff0c;但是苦于都要积分&#xff0c;出于服务大众、便利人民的心。 我终于找到了对应的下载磁力…

RedHat使用yum下载安装软件包

RedHat使用yum下载安装软件包 Red Hat Enterprise Linux Server&#xff08;RHEL&#xff09;的yum服务是收费的&#xff0c;如果没有付费&#xff0c;则无法使用yum安装软件包。通过删除RedHat自带的yum&#xff0c;安装CentOS版本的yum&#xff0c;并使用CentOS的yum源和epel…

RedHat红帽RHEL7.2镜像下载以及安装教程(内含下载链接)

RedHat红帽RHEL7.2镜像下载以及安装教程 镜像下载链接&#xff1a; https://pan.baidu.com/s/1czcz-ClYavcugE9PJDIIQg?pwdq2t1 提取码:q2t1 安装教程 1、打开VM&#xff0c;新建虚拟机 2、选择自定义&#xff0c;下一步 3、默认&#xff0c;下一步 4、选择稍后安装操作系统…

redhat官方文档下载方法

打开地址&#xff1a;https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/7/ 如图下载pdf格式文件&#xff1a;

安装Redhat

1.新建虚拟机&#xff0c;选典型 2.下一步&#xff0c;选择稍后安装操作系统 3.下一步&#xff0c;选择Linux&#xff0c;版本选择Red Hat Enterprise 8&#xff08;版本是什么就选择什么&#xff09; 4.下一步&#xff0c;设置虚拟机名称以及位置 5.下一步&#xff0c;设…

红帽linux6.8镜像下载,redhat8镜像下载

https://developers.redhat.com/rhel8 点击下图的“Download RHEL” 点击完 “Download RHEL”后会跳转到登录界面&#xff0c;注册个账号登录即可&#xff0c;该文档将跳过注册步骤 截止该文档下载时&#xff0c;redhat8的版本为8.3 beat版本&#xff0c;点击“View Older Dow…

redhat下载gcc

redhat似乎受众很少&#xff0c;安装gcc很少有参照。 倒腾很久终于安装成功了&#xff0c;浅浅记录一下。 1、首先将创建一个gcc文件夹&#xff0c;将gcc挂载到/dev/sr0下 2、然后进入繁琐的安装过程。 &#xff08;大抵是需要这些包了&#xff0c;但是不同rhel可能版本号不同…

如何下载redhat enterprise版本

1.登录网站: developers.redhat.com 2.点开下面红框内的图标 3.再点下面红框内的图标 4. 继续点红框内的图标 5.点开以后是下面的页面&#xff0c;往下拖动就可以看见redhat enterprise 的很多版本了 备注&#xff1a;以上网站有时候下载一段时间就断开不能下载了。也可以从下…

Red Hat Enterprise Linux RHEL 8.6 下载安装

前言 由于 CentOS 长期以来没有为 Red Hat 增加价值&#xff0c;Red Hat 停止了 CentOS Linux 的维护&#xff0c;为了留住小规模 CentOS 用户&#xff0c;Red Hat 允许免费下载 RHEL&#xff0c;在 2021 年 1 月&#xff0c;红帽宣布个人可以获得免费的个人订阅&#xff0c;以…

red hat 系统下载

red hat linux 系统下载 在官方下载需要有账号&#xff0c;如果没有账号可到百度云下载&#xff08;百度云的速度你知道&#xff09; https://pan.baidu.com/s/1gRuUdTFqnKP9a4yy6y2rbg 官方下载地址&#xff1a;https://developers.redhat.com/products/rhel/download/ 1.…

Java算法与数据结构、设计模式、高并发视频教程免费下载

Java算法与数据结构、设计模式、高并发视频教程免费下载&#xff01; 链接&#xff1a;http://pan.baidu.com/s/1gfyobmF 密码&#xff1a;bef5 链接我就不放出来了&#xff0c;太容易失效。需要这套视频教的网页&#xff0c;可以扫描下方的微信二维码&#xff0c;关注“业余…

Python数据结构与算法视频教程-王宁宁-专题视频课程

Python数据结构与算法视频教程—367人已学习 课程介绍 Python数据结构与算法视频培训教程&#xff1a;本课程内容包含了程序员常用的数据结构知识&#xff0c;涉及快速排序、树与二叉树、堆、堆排序、图的概念与遍历、Python常用的内置算法与数据结构等开发知识。数据结构和…