RadioButton使用方法

2024-09-04 22:18
文章标签 使用 方法 radiobutton

本文主要是介绍RadioButton使用方法,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

学一点,记一点。

要点

  • 一组Radio Button放到一个Radio Group中;
  • 如果Radio Button很多,就考虑是否Spinner更合适
  • 事件处理有多种方式,如RadioGroup的OnCheckedChangeListener。

示例

效果

ui

功能

选择某一个Radio Button之后,下面一个TextView显示选择结果。

布局文件 my_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><RadioGroupandroid:id="@+id/radio_group"android:layout_width="457dp"android:layout_height="wrap_content"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/radio_first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="@string/first" /><RadioButtonandroid:id="@+id/radio_second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/second" /><RadioButtonandroid:id="@+id/radio_third"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/third" /></RadioGroup><TextViewandroid:id="@+id/select_result"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

代码

省略掉自动生成的代码。

private RadioGroup radioGroup = null;
private TextView textView = null;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);radioGroup = (RadioGroup) this.findViewById(R.id.radio_group);textView = (TextView) this.findViewById(R.id.select_result);textView.setText(getSelectedInfo());radioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {public void onCheckedChanged(RadioGroup group, int checkedId) {textView.setText(getSelectedInfo(checkedId));}});}private String getSelectedInfo() {int checkedId = radioGroup.getCheckedRadioButtonId();return getSelectedInfo(checkedId);
}private String getSelectedInfo(int checkedId) {String selected = "Unknown selection!";switch (checkedId) {case R.id.radio_first:selected = "First was selected.";break;case R.id.radio_second:selected = "Second was selected.";break;case R.id.radio_third:selected = "Third was selected.";break;default:break;}return selected;
}

事件处理

上面的示例是为RadioGroup对象注册OnCheckedChangeListener。此外,还可以直接为每个Radio Button注册listener。接下来详细描述。

RadioButton事件处理

可以为Radio Button注册CompoundButton.OnCheckedChangeListener。

代码一

private RadioButton firstRadio = null;
private RadioButton secondRadio = null;
private RadioButton thirdRadio = null;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);firstRadio = (RadioButton) this.findViewById(R.id.radio_first);secondRadio = (RadioButton) this.findViewById(R.id.radio_second);thirdRadio = (RadioButton) this.findViewById(R.id.radio_third);firstRadio.setOnCheckedChangeListener(new MyListener("First"));secondRadio.setOnCheckedChangeListener(new MyListener("Second"));thirdRadio.setOnCheckedChangeListener(new MyListener("Third"));
}private class MyListener implements CompoundButton.OnCheckedChangeListener {private String radioName = null;public MyListener(String theRadioName) {radioName = theRadioName;}public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {Toast.makeText(MainActivity.this, radioName + " checked:" + isChecked, Toast.LENGTH_SHORT).show();}
}

实际运行发现,被选中的Radio Button会先处理,显示:xxx checked: true;失去焦点的Radio Button之后处理,显示:xxx checked: false。

注意到,这里通过ctor传入一个标识符,说明当前的事件处理是哪个Radio Button的。显然,这种方式的通用性并不好。

代码二

下面是等价的一种方式:

private RadioButton firstRadio = null;
private RadioButton secondRadio = null;
private RadioButton thirdRadio = null;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);firstRadio = (RadioButton) this.findViewById(R.id.radio_first);secondRadio = (RadioButton) this.findViewById(R.id.radio_second);thirdRadio = (RadioButton) this.findViewById(R.id.radio_third);MyListener listener = new MyListener();firstRadio.setOnCheckedChangeListener(listener);secondRadio.setOnCheckedChangeListener(listener);thirdRadio.setOnCheckedChangeListener(listener);
}private String getRadioName(int id) {switch (id) {case R.id.radio_first: return "First";case R.id.radio_second: return "Second";case R.id.radio_third: return "Third";default: return "Unknown";}
}private class MyListener implements CompoundButton.OnCheckedChangeListener {public void onCheckedChanged(CompoundButton buttonView,boolean isChecked) {String radioName = getRadioName(buttonView.getId());Toast.makeText(MainActivity.this, radioName + " checked:" + isChecked, Toast.LENGTH_SHORT).show();}
}

xml中定义事件

在docs/guide/topics/ui/controls/radiobutton.html中提到在xml中定义Radio Button的事件处理。

xml

在每个RadioButton中增加一个属性:

android:onClick="onRadioButtonClicked"

即变成下面的样子:

<RadioGroupandroid:id="@+id/radio_group"android:layout_width="457dp"android:layout_height="wrap_content"android:orientation="horizontal" ><RadioButtonandroid:id="@+id/radio_first"android:layout_width="wrap_content"android:layout_height="wrap_content"android:checked="true"android:text="@string/first"android:onClick="onRadioButtonClicked" /><RadioButtonandroid:id="@+id/radio_second"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/second"android:onClick="onRadioButtonClicked" /><RadioButtonandroid:id="@+id/radio_third"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="@string/third"android:onClick="onRadioButtonClicked" />
</RadioGroup>

代码

@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);
}private String getRadioName(int id) {switch (id) {case R.id.radio_first: return "First";case R.id.radio_second: return "Second";case R.id.radio_third: return "Third";default: return "Unknown";}
}public void onRadioButtonClicked(View view) {boolean checked = ((RadioButton) view).isChecked();String radioName = getRadioName(view.getId());Toast.makeText(this, radioName + " checked:" + checked, Toast.LENGTH_SHORT).show();
}

运行效果

当选中某个Radio Button的时候,会触发事件。失去焦点的不会触发事件,——因为是OnClick类型的事件,而不是OnCheckedChanged。

所以,这种方法比前一种要简洁得多。

在代码中注册OnClickListener

另外一种等价方式就是直接在代码中为RadioButton注册OnClickListener对象。

为此,在xml中删除前面添加的属性,然后代码改成如下的样子:

private RadioButton firstRadio = null;
private RadioButton secondRadio = null;
private RadioButton thirdRadio = null;@Override
protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.my_layout);OnClickListener listener = new OnClickListener() {public void onClick(View view) {boolean checked = ((RadioButton) view).isChecked();String radioName = getRadioName(view.getId());Toast.makeText(MainActivity.this, radioName + " checked:" + checked, Toast.LENGTH_SHORT).show();}};firstRadio = (RadioButton) this.findViewById(R.id.radio_first);secondRadio = (RadioButton) this.findViewById(R.id.radio_second);thirdRadio = (RadioButton) this.findViewById(R.id.radio_third);firstRadio.setOnClickListener(listener);secondRadio.setOnClickListener(listener);thirdRadio.setOnClickListener(listener);
}private String getRadioName(int id) {switch (id) {case R.id.radio_first: return "First";case R.id.radio_second: return "Second";case R.id.radio_third: return "Third";default: return "Unknown";}
}

对比分析

以下两种方法比较好:

  • 为Radio Group注册OnCheckedChangeListener
  • 为Radio Button注册OnClickListener

注意点:OnXyzListener会在不同的class中存在,有时候需要认真区分,必要情况下用ClassName.OnXyzListener的方式来引用。

这篇关于RadioButton使用方法的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1137185

相关文章

Spring Boot整合Redis注解实现增删改查功能(Redis注解使用)

《SpringBoot整合Redis注解实现增删改查功能(Redis注解使用)》文章介绍了如何使用SpringBoot整合Redis注解实现增删改查功能,包括配置、实体类、Repository、Se... 目录配置Redis连接定义实体类创建Repository接口增删改查操作示例插入数据查询数据删除数据更

使用python生成固定格式序号的方法详解

《使用python生成固定格式序号的方法详解》这篇文章主要为大家详细介绍了如何使用python生成固定格式序号,文中的示例代码讲解详细,具有一定的借鉴价值,有需要的小伙伴可以参考一下... 目录生成结果验证完整生成代码扩展说明1. 保存到文本文件2. 转换为jsON格式3. 处理特殊序号格式(如带圈数字)4

Java使用Swing生成一个最大公约数计算器

《Java使用Swing生成一个最大公约数计算器》这篇文章主要为大家详细介绍了Java使用Swing生成一个最大公约数计算器的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以了解一下... 目录第一步:利用欧几里得算法计算最大公约数欧几里得算法的证明情形 1:b=0情形 2:b>0完成相关代码第二步:加

Java中流式并行操作parallelStream的原理和使用方法

《Java中流式并行操作parallelStream的原理和使用方法》本文详细介绍了Java中的并行流(parallelStream)的原理、正确使用方法以及在实际业务中的应用案例,并指出在使用并行流... 目录Java中流式并行操作parallelStream0. 问题的产生1. 什么是parallelS

MySQL数据库双机热备的配置方法详解

《MySQL数据库双机热备的配置方法详解》在企业级应用中,数据库的高可用性和数据的安全性是至关重要的,MySQL作为最流行的开源关系型数据库管理系统之一,提供了多种方式来实现高可用性,其中双机热备(M... 目录1. 环境准备1.1 安装mysql1.2 配置MySQL1.2.1 主服务器配置1.2.2 从

Linux join命令的使用及说明

《Linuxjoin命令的使用及说明》`join`命令用于在Linux中按字段将两个文件进行连接,类似于SQL的JOIN,它需要两个文件按用于匹配的字段排序,并且第一个文件的换行符必须是LF,`jo... 目录一. 基本语法二. 数据准备三. 指定文件的连接key四.-a输出指定文件的所有行五.-o指定输出

Linux jq命令的使用解读

《Linuxjq命令的使用解读》jq是一个强大的命令行工具,用于处理JSON数据,它可以用来查看、过滤、修改、格式化JSON数据,通过使用各种选项和过滤器,可以实现复杂的JSON处理任务... 目录一. 简介二. 选项2.1.2.2-c2.3-r2.4-R三. 字段提取3.1 普通字段3.2 数组字段四.

Linux kill正在执行的后台任务 kill进程组使用详解

《Linuxkill正在执行的后台任务kill进程组使用详解》文章介绍了两个脚本的功能和区别,以及执行这些脚本时遇到的进程管理问题,通过查看进程树、使用`kill`命令和`lsof`命令,分析了子... 目录零. 用到的命令一. 待执行的脚本二. 执行含子进程的脚本,并kill2.1 进程查看2.2 遇到的

详解SpringBoot+Ehcache使用示例

《详解SpringBoot+Ehcache使用示例》本文介绍了SpringBoot中配置Ehcache、自定义get/set方式,并实际使用缓存的过程,文中通过示例代码介绍的非常详细,对大家的学习或者... 目录摘要概念内存与磁盘持久化存储:配置灵活性:编码示例引入依赖:配置ehcache.XML文件:配置

Java 虚拟线程的创建与使用深度解析

《Java虚拟线程的创建与使用深度解析》虚拟线程是Java19中以预览特性形式引入,Java21起正式发布的轻量级线程,本文给大家介绍Java虚拟线程的创建与使用,感兴趣的朋友一起看看吧... 目录一、虚拟线程简介1.1 什么是虚拟线程?1.2 为什么需要虚拟线程?二、虚拟线程与平台线程对比代码对比示例:三