阅读《Android 从入门到精通》(10)——单项选择

2024-03-01 02:38

本文主要是介绍阅读《Android 从入门到精通》(10)——单项选择,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

单项选择(RadioGroup)

RadioGroup 是 LinearLayout 的子类,继承关系如下:

android.view.ViewGroup
android.widget.LinearLayout
android.widget.RadioGroup

RadioGroup 类方法


RadioGroup 示例

完整工程:http://download.csdn.net/detail/sweetloveft/9402088
下述程序中,主要学习 RadioGroup 的用法,需要注意单选按钮的处理逻辑是通过监听 RadioGroup 完成的,具体处理哪个单选按钮则是通过 getId() 完成的。

1.MainActivity.java

package com.sweetlover.activity;import com.sweetlover.radiogroupdemo.R;import android.app.Activity;
import android.os.Bundle;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Toast;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.TextView;public class MainActivity extends Activity {private static final int BTN_NUM = 6;private TextView textView = null;private RadioGroup radioGroup = null;private RadioButton[] radioButton = null;@Overrideprotected void onCreate(Bundle savedInstanceState) {// TODO Auto-generated method stubsuper.onCreate(savedInstanceState);setContentView(R.layout.activity_main);radioButton = new RadioButton[BTN_NUM];textView = (TextView)findViewById(R.id.textView1);radioGroup = (RadioGroup)findViewById(R.id.radioGroup);radioButton[0] = (RadioButton)findViewById(R.id.radioButton1);radioButton[1] = (RadioButton)findViewById(R.id.radioButton2);radioButton[2] = (RadioButton)findViewById(R.id.radioButton3);radioButton[3] = (RadioButton)findViewById(R.id.radioButton4);radioButton[4] = (RadioButton)findViewById(R.id.radioButton5);radioButton[5] = (RadioButton)findViewById(R.id.radioButton6);radioGroup.setOnCheckedChangeListener(new RadioGroupCheckedListener());}class RadioGroupCheckedListener implements OnCheckedChangeListener {@Overridepublic void onCheckedChanged(RadioGroup group, int checkedId) {// TODO Auto-generated method stubfor (int i = 0; i < BTN_NUM; i++) {if (checkedId == radioButton[i].getId() && radioButton[i].isChecked()) {CharSequence text = radioButton[i].getText();textView.setText(text);Toast.makeText(MainActivity.this, text, Toast.LENGTH_SHORT).show();break;}}}}
}

2.activity_main.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:padding="30dp"android:orientation="vertical" ><TextViewandroid:id="@+id/textView1"android:layout_width="wrap_content"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/system"android:textAppearance="?android:attr/textAppearanceMedium" /><RadioGroupandroid:id="@+id/radioGroup"android:layout_width="match_parent"android:layout_height="wrap_content"android:padding="30dp" ><RadioButtonandroid:id="@+id/radioButton1"android:layout_width="160dp"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/os1" /><RadioButtonandroid:id="@+id/radioButton2"android:layout_width="160dp"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/os2" /><RadioButtonandroid:id="@+id/radioButton3"android:layout_width="160dp"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/os3" /><RadioButtonandroid:id="@+id/radioButton4"android:layout_width="160dp"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/os4" /><RadioButtonandroid:id="@+id/radioButton5"android:layout_width="160dp"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/os5" /><RadioButtonandroid:id="@+id/radioButton6"android:layout_width="160dp"android:layout_height="wrap_content"android:layout_gravity="center"android:text="@string/os6" /></RadioGroup></LinearLayout>

3.string.xml

<resources><string name="app_name">RadioGroupDemo</string><string name="system">操作系统</string><string name="os1">Android</string><string name="os2">Sysbian</string><string name="os3">WinCE</string><string name="os4">PalmOS</string><string name="os5">Linux</string><string name="os6">iPhoneOS</string></resources>

4.AndroidManifest.xml

<manifest xmlns:android="http://schemas.android.com/apk/res/android"package="com.sweetlover.radiogroupdemo"android:versionCode="1"android:versionName="1.0" ><uses-sdkandroid:minSdkVersion="8"android:targetSdkVersion="19" /><applicationandroid:allowBackup="true"android:icon="@drawable/ic_launcher"android:label="@string/app_name"android:theme="@style/AppTheme" ><activity android:name="com.sweetlover.activity.MainActivity"><intent-filter><action android:name="android.intent.action.MAIN"/><category android:name="android.intent.category.LAUNCHER"/></intent-filter></activity></application></manifest>

这篇关于阅读《Android 从入门到精通》(10)——单项选择的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!


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

相关文章

Android NDK版本迭代与FFmpeg交叉编译完全指南

《AndroidNDK版本迭代与FFmpeg交叉编译完全指南》在Android开发中,使用NDK进行原生代码开发是一项常见需求,特别是当我们需要集成FFmpeg这样的多媒体处理库时,本文将深入分析A... 目录一、android NDK版本迭代分界线二、FFmpeg交叉编译关键注意事项三、完整编译脚本示例四

Android与iOS设备MAC地址生成原理及Java实现详解

《Android与iOS设备MAC地址生成原理及Java实现详解》在无线网络通信中,MAC(MediaAccessControl)地址是设备的唯一网络标识符,本文主要介绍了Android与iOS设备M... 目录引言1. MAC地址基础1.1 MAC地址的组成1.2 MAC地址的分类2. android与I

POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能

《POI从入门到实战轻松完成EasyExcel使用及Excel导入导出功能》ApachePOI是一个流行的Java库,用于处理MicrosoftOffice格式文件,提供丰富API来创建、读取和修改O... 目录前言:Apache POIEasyPoiEasyExcel一、EasyExcel1.1、核心特性

exfat和ntfs哪个好? U盘格式化选择NTFS与exFAT的详细区别对比

《exfat和ntfs哪个好?U盘格式化选择NTFS与exFAT的详细区别对比》exFAT和NTFS是两种常见的文件系统,它们各自具有独特的优势和适用场景,以下是关于exFAT和NTFS的详细对比... 无论你是刚入手了内置 SSD 还是便携式移动硬盘或 U 盘,都需要先将它格式化成电脑或设备能够识别的「文

Android 实现一个隐私弹窗功能

《Android实现一个隐私弹窗功能》:本文主要介绍Android实现一个隐私弹窗功能,本文通过实例代码给大家介绍的非常详细,感兴趣的朋友一起看看吧... 效果图如下:1. 设置同意、退出、点击用户协议、点击隐私协议的函数参数2. 《用户协议》、《隐私政策》设置成可点击的,且颜色要区分出来res/l

Android实现一键录屏功能(附源码)

《Android实现一键录屏功能(附源码)》在Android5.0及以上版本,系统提供了MediaProjectionAPI,允许应用在用户授权下录制屏幕内容并输出到视频文件,所以本文将基于此实现一个... 目录一、项目介绍二、相关技术与原理三、系统权限与用户授权四、项目架构与流程五、环境配置与依赖六、完整

Android 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚