无涯教程-Android - RadioButton函数

RadioButton有两种状态:选中或未选中,这允许用户从一组中选择一个选项。

Radio Button

Radio Button

示例

本示例将带您完成一些简单的步骤,以展示如何使用Linear Layout和RadioButton创建自己的Android应用程序。

以下是修改后的主要Activity文件 src/MainActivity.java 的内容。

package com.example.saira_000.myapplication;import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;public class MainActivity extends ActionBarActivity {RadioGroup rg1;RadioButton rb1;Button b1;protected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);addListenerRadioButton();}private void addListenerRadioButton() {rg1 = (RadioGroup) findViewById(R.id.radioGroup);b1 = (Button) findViewById(R.id.button2);b1.setOnClickListener(new View.OnClickListener() {@Overridepublic void onClick(View v) {int selected=rg1.getCheckedRadioButtonId();rb1=(RadioButton)findViewById(selected);Toast.makeText(MainActivity.this,rb1.getText(),Toast.LENGTH_LONG).show();}});}
}

以下是 res/layout/activity_main.xml 文件的内容-

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:tools="http://schemas.android.com/tools"android:layout_width="match_parent"android:layout_height="match_parent"android:paddingBottom="@dimen/activity_vertical_margin"android:paddingLeft="@dimen/activity_horizontal_margin"android:paddingRight="@dimen/activity_horizontal_margin"android:paddingTop="@dimen/activity_vertical_margin"tools:context=".MainActivity"><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Example of Radio Button"android:layout_alignParentTop="true"android:layout_centerHorizontal="true"android:textSize="30dp" /><TextViewandroid:id="@+id/textView2"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Tutorials point"android:textColor="#ff87ff09"android:textSize="30dp"android:layout_above="@+id/imageButton"android:layout_centerHorizontal="true"android:layout_marginBottom="40dp" /><ImageButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/imageButton"android:src="@drawable/abc"android:layout_centerVertical="true"android:layout_centerHorizontal="true" /><Buttonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:id="@+id/button2"android:text="ClickMe"android:layout_alignParentBottom="true"android:layout_centerHorizontal="true" /><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="fill_parent"android:layout_height="fill_parent"android:layout_below="@+id/imageButton"android:layout_alignLeft="@+id/textView2"android:layout_alignStart="@+id/textView2"><RadioButtonandroid:layout_width="142dp"android:layout_height="wrap_content"android:text="JAVA"android:id="@+id/radioButton"android:textSize="25dp"android:textColor="@android:color/holo_red_light"android:checked="false"android:layout_gravity="center_horizontal" /><RadioButtonandroid:layout_width="wrap_content"android:layout_height="wrap_content"android:text="ANDROID"android:id="@+id/radioButton2"android:layout_gravity="center_horizontal"android:checked="false"android:textColor="@android:color/holo_red_dark"android:textSize="25dp" /><RadioButtonandroid:layout_width="136dp"android:layout_height="wrap_content"android:text="HTML"android:id="@+id/radioButton3"android:layout_gravity="center_horizontal"android:checked="false"android:textSize="25dp"android:textColor="@android:color/holo_red_dark" /></RadioGroup></RelativeLayout>

以下是 res/values/strings.xml 的内容,以定义这些新常量-

<?xml version="1.0" encoding="utf-8"?>
<resources><string name="app_name">My Application</string>
</resources>

以下是 AndroidManifest.xml 的默认内容-

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.example.saira_000.myapplication" ><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activityandroid:name="com.example.My Application.MainActivity"android:label="@string/app_name" ><intent-filter><action android:name="android.intent.action.MAIN" /><category android:name="android.intent.category.LAUNCHER" /></intent-filter></activity></application>
</manifest>

单击运行Eclipse Run Icon工具栏。 Android Studio将应用程序安装在您的AVD上并启动它,如果您的设置和应用程序一切正常,它将显示在"Emulator"窗口下面-

Android RadioButton Control

如果用户选择了任何一个单选按钮,则它应该在Toast消息中使用相同的名称。例如,如果用户选择了JAVA,它将给出一个消息,即JAVA

Android 中的 RadioButton函数 - 无涯教程网无涯教程网提供RadioButton有两种状态:选中或未选中,这允许用户从一组中选择一个选项。 Radio Button...https://www.learnfk.com/android/android-radiobutton-control.html

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.hqwc.cn/news/94136.html

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Redis 7 第五讲 事务、管道、发布订阅 过渡篇

事务 理论 可以一次执行多个命令&#xff0c;本质是一组命令的集合。一个事务中的所有命令都会序列化&#xff0c;按顺序地串行化执行而不会被其它命令插入&#xff0c;不许加塞 一个队列中&#xff0c;一次性、顺序性、排他性的执行一系列命令 Redis事务 VS 关系型数据库事务…

【CSS】简记CSS效果:通过transition(动画过渡属性)实现侧边栏目滑入滑出

需求 在资金明细的页面中&#xff0c;点击按钮时筛选区域从左侧滑出&#xff0c;完成筛选点击确认后调用接口完成数据查询&#xff0c;筛选区域滑入左侧&#xff1b; 基于微信小程序页面实现 wxml代码 <view><!-- 操作按钮 --><button type"primary&qu…

艾昆纬携手亚马逊云科技赋能生物医药企业,加速武田数字化转型

IQVIA艾昆纬是全球以及中国医疗健康服务领域的领导者&#xff0c;利用其数据及分析&#xff0c;前沿数字化技术和医疗领域的专业知识&#xff0c;智能连接医疗生态的各个环节。过去几年&#xff0c;IQVIA艾昆纬所服务的生物医药行业客户武田中国也迎来了数字化转型的高潮。 武田…

视频智能分析平台EasyCVR安防视频汇聚平台助力森林公园防火安全的应用方案

一、研发背景 随着经济的发展和人们生活水平的提高&#xff0c;越来越多的人喜欢在周末去周边的森林公园旅游&#xff0c;享受大自然的美景&#xff0c;并进行野炊和烧烤等娱乐活动。然而&#xff0c;近年来由于烟蒂和烧烤碳渣等人为因素&#xff0c;森林公园火灾频繁发生。森…

【Java 动态数据统计图】动态数据统计思路案例(动态,排序,动态数组(重点推荐))七(129)

需求&#xff1a;前端根据后端的返回数据&#xff1a;画统计图&#xff1b; 说明&#xff1a; 1.X轴为地域&#xff0c;Y轴为地域出现的次数&#xff1b; 2. 动态展示&#xff08;有地域展示&#xff0c;没有不展示&#xff0c;且高低排序&#xff09; Demo案例&#xff1a; …

Ansible学习笔记11

Command和Shell模块&#xff1a; 两个模块都是用于执行Linux命令的&#xff0c;这个对于命令熟悉的工程师来说&#xff0c;用起来非常high。 Shell模块跟Command模块差不多&#xff08;Command模块不能执行一类$HOME、> 、<、| 等符号&#xff0c;但是Shell是可以的。&…

【Mycat 2】详解分库分表设计方案及实操测试(二)

文章目录 4. 分表、不分实例、不分库4.1 数据分片映射关系4.2 测试过程5. 分实例、分表、不分库5.1 分库键和分表键相同5.1.1 数据分片映射关系5.1.2 测试过程5.2 分库键和分表键不相同5.2.1 数据分片映射关系5.2.2 测试过程6. 不分片6.1 普通表6.1.1 数据映射关系6.1.2 测试过…

本地部署 CodeLlama 并在 VSCode 中使用 CodeLlama

本地部署 CodeLlama 并在 VSCode 中使用 CodeLlama 1. CodeLlama 是什么2. CodeLlama Github 地址3. 下载 CodeLlama 模型4. 部署 CodeLlama5. 在 VSCode 中使用 CodeLlama6. 使用WSGI启动服务7. 创建 start.sh 启动脚本 1. CodeLlama 是什么 Code Llama 是一个基于 Llama 2 的…

Navicat 强大的数据模型功能 | 面向数据库设计、架构和数据资产梳理等使用场景

数据模型是用来描述数据、组织数据和对数据进行操作的一组概念和定义。根据不同的应用需求&#xff0c;数据模型可以分为概念模型、逻辑模型和物理模型。这些数据模型帮助数据库设计人员设计和管理数据库&#xff0c;以满足用户的需求。 Navicat 强大的数据模型功能主要适用于…

Linux内核源码分析 (A)常见内核面试题

Linux内核源码分析 (A)常见内核面试题 文章目录 Linux内核源码分析 (A)常见内核面试题调用 schedule() 进行进程切换的方式有几种CFS调度器vruntime的计算方式 网站收集面试题集合1 调用 schedule() 进行进程切换的方式有几种 系统调用 do_fork()&#xff1a;copy_process()定…

用idea查看sqlite数据库idea sqlite

1、安装Database Navigator插件 2、导入数据库并查看 3、删除数据库连接 在此做个笔记

LabVIEW液压支架控制系统的使用与各种配置的预测模型的比较分析

LabVIEW液压支架控制系统的使用与各种配置的预测模型的比较分析 模型预测控制在工业中应用广泛。这种方法的优点之一是在求解最优控制问题时能够明确考虑对输入和输出状态施加的约束。控制对象模型用于有限时间范围内最优控制的实时计算。所使用的数学设备允许从具有单输入和单…