无涯教程-Android - RadioButton函数

news2024/10/7 10:21:12

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() {
         @Override
         public 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">
   
   <TextView
      android: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" />
      
   <TextView
      android: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" />
      
   <ImageButton
      android: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" />
      
   <Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/button2"
      android:text="ClickMe"
      android:layout_alignParentBottom="true"
      android:layout_centerHorizontal="true" />
   
   <RadioGroup
      android: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">
         
      <RadioButton
         android: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" />
         
      <RadioButton
         android: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" />
         
      <RadioButton
         android: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" >
   
   <application
      android:allowBackup="true"
      android:icon="@drawable/ic_launcher"
      android:label="@string/app_name"
      android:theme="@style/AppTheme" >
      
      <activity
         android: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.coloradmin.cn/o/956844.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【算法】函数渐近的界基础知识及定理

创作不易&#xff0c;本篇文章如果帮助到了你&#xff0c;还请点赞 关注支持一下♡>&#x16966;<)!! 主页专栏有更多知识&#xff0c;如有疑问欢迎大家指正讨论&#xff0c;共同进步&#xff01; &#x1f525;c系列专栏&#xff1a;C/C零基础到精通 &#x1f525; 给大…

Spring依赖注入(DI)

目录 构造器注入 set注入 拓展注入 bean的作用域 Singleton Prototype Dependency Injection 依赖 : 指Bean对象的创建依赖于容器 . Bean对象的依赖资源 . 注入 : 指Bean对象所依赖的资源 , 由容器来设置和装配 . 构造器注入 具体实现&#xff1a;SpringIOC创建对象的…

电力行业浪涌保护器应用方案

电力行业是一个涉及到高压、大电流、复杂环境的领域&#xff0c;对于电气设备的安全和可靠性有着极高的要求。在电力行业中&#xff0c;浪涌是一种常见的电力质量问题&#xff0c;它指的是在电气系统中出现的瞬时过电压或过电流&#xff0c;可能由雷击、开关操作、短路故障等因…

算法通关村——解析堆的应用

在数组中找第K大的元素 LeetCode21 Medium 我们要找第 K 大的元素&#xff0c;如果我们找使用大堆的话那么就会造成这个堆到底需要多大的&#xff0c;而且哪一个是第 K 的的元素我们不知道是哪一个索引&#xff0c;我们更想要的结果就是根节点就是我们要找的值&#xff0c;所以…

java之SpringBoot基础、前后端项目、MyBatisPlus、MySQL、vue、elementUi

文章目录 前言JC-1.快速上手SpringBootJC-1-1.SpringBoot入门程序制作&#xff08;一&#xff09;JC-1-2.SpringBoot入门程序制作&#xff08;二&#xff09;JC-1-3.SpringBoot入门程序制作&#xff08;三&#xff09;JC-1-4.SpringBoot入门程序制作&#xff08;四&#xff09;…

cocosCreator 之 微信小游戏打包

版本&#xff1a; v3.8.0 环境&#xff1a; Mac 介绍 cocosCreator 支持将游戏发布到多个小游戏平台&#xff0c;并提供了打包等流程处理。 本篇文章主要讲述下微信小游戏的发布流程相关。更多内容参考官方文档&#xff1a; 发布到小游戏平台 微信小游戏的发布相关&#xff…

2023-8-31 Floyd求最短路

题目链接&#xff1a;Floyd求最短路 #include <iostream> #include <algorithm> #include <cstring>using namespace std;const int N 210, INF 1e9;int n, m, Q;int d[N][N];void floyd() {for(int k 1; k < n; k)for(int i 1; i < n; i)for(int …

并发编程的故事——并发之共享模型

并发之共享模型 文章目录 并发之共享模型一、多线程带来的共享问题二、解决方案三、方法中的synchronize四、变量的线程安全分析五、习题六、Monitor七、synchronize优化八、wait和notify九、sleep和wait十、park和unpark十一、重新理解线程状态十二、多把锁十三、ReentrantLoc…

瓜分双十一10亿红包设计:在线分享教程?

在如今激烈的市场竞争中&#xff0c;瓜分红包营销活动成为了各大企业争相使用的一种营销手段。这种活动不仅能够吸引用户的关注和参与&#xff0c;还能够提高用户的粘性和忠诚度。那么&#xff0c;如何自建瓜分红包营销活动呢&#xff1f;下面将为大家详细解析。 首先&#xff…

振动智能监测与设备可靠性:无线技术的契机

在现代工业领域&#xff0c;设备的可靠性和稳定运行对于生产效率和安全性至关重要。然而&#xff0c;由于设备的频繁使用和各种环境影响&#xff0c;设备故障和突发停机仍然是不可避免的挑战。为了有效地应对这些挑战&#xff0c;振动智能监测技术结合无线传感器的应用正在成为…

FPGA可重配置原理及实现(1)——导论

一、概述 可重配置技术是Xilinx提供的用来高效利用FPGA设计资源实现FPGA资源可重复利用的最新的FPGA设计技术&#xff0c;这种技术的发展为FPGA应用提供了更加广阔的前景。 术语“重构”是指FPGA已经配置后的重新编程。FPGA的重构有两种类型&#xff1a;完全的和部分的。完全重…

【附安装包】Substance3D 2022安装教程

软件下载 软件&#xff1a;Substance3D版本&#xff1a;2022语言&#xff1a;简体中文大小&#xff1a;4.0G安装环境&#xff1a;Win11/Win10&#xff08;1809版本以上&#xff09;硬件要求&#xff1a;CPU2.0GHz 内存4G(或更高&#xff0c;不支持7代以下CPU&#xff09;下载通…

即时通讯开发应用中的实时消息推送技术

即时通讯开发领域正以前所未有的速度蓬勃发展&#xff0c;实时消息推送技术成为促进即时通讯应用体验的关键要素。本文将深入探讨即时通讯应用中的实时消息推送技术&#xff0c;为读者呈现这一领域的全貌。 2. 实时消息推送的重要性 在当今数字化时代&#xff0c;人们日益需要…

git 提交错误,回滚到某一个版本

git log 查看版本号 commit 后面跟的就是版本号git reset --hard 版本号 &#xff08;就可以回滚到你要去的版本&#xff09;git push -f &#xff08;因为本地回滚了&#xff0c;所以和远程会差几个版本。所以这时候只有强制推送&#xff0c;覆盖远程才可以&#xff09;

实力认证!OceanBase获“鼎信杯”优秀技术支撑奖

6 月 30 日&#xff0c;2023 “鼎信杯”信息技术发展论坛在京隆重举办第二届“鼎信杯”大赛颁奖典礼。OceanBase 凭借完全自主研发的原生分布式数据库&#xff0c;以及丰富的核心系统国产数据库升级案例&#xff0c;斩获“优秀技术支撑奖”。 论坛上&#xff0c;国内首个基于在…

JavaScript基础03

JavaScript 基础 文章目录 JavaScript 基础for 语句for语句的基本使用循环嵌套倒三角九九乘法表 数组数组是什么&#xff1f;数组的基本使用定义数组和数组单元访问数组和数组索引数据单元值类型数组长度属性 操作数组 if 多分支语句和 switch的区别&#xff1a; 共同点 都能实…

C语言如何判断闰年?

首先需要了解闰年的判断规则&#xff0c;以下是百度百科的介绍&#xff1a; 1.普通年份能被4整除&#xff0c;且不能被100整除的&#xff0c;是闰年。&#xff08;如2004年就是闰年&#xff09; 2.世纪年份能被400整除的是闰年。&#xff08;如2000年是闰年&#xff0c;1900年不…

JVM-CMS

when 堆大小要求为4-8G 原理 初始标记&#xff1a;执行CMS线程->STW&#xff0c;标记GC Root直接关联的对象->低延迟 并发标记&#xff1a;执行CMS线程和业务线程&#xff0c;从GC Root直接关联的对象开始遍历整个对象图 重新标记&#xff1a;执行CMS线程->STW&a…

【炼气境】Java集合框架篇

【炼气境】Java集合框架篇 文章目录 【炼气境】Java集合框架篇概述接口Collection接口List接口ArrayList类LinkedList类 Set接口HashSet类LinkedHashSet类TreeSet类 Queue接口LinkedList类PriorityQueue类ArrayDeque Map接口HashMap类LinkedHashMap类TreeMap类 常用方法特性适用…

有点意思的 Java 递归调用

最近在刷一些问题的时候看到有下面一个问题 上面问的是当输入的字符串为什么的时候返回 True 总结 在做题目的时候&#xff0c;第一次还做错了。 这是因为解答这个题目的时间只有 3 分钟&#xff0c;没有自己看题目 后来拿着程序跑了下。 public void testGetPut() throws …