阅读《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

相关文章

从入门到精通详解Python虚拟环境完全指南

《从入门到精通详解Python虚拟环境完全指南》Python虚拟环境是一个独立的Python运行环境,它允许你为不同的项目创建隔离的Python环境,下面小编就来和大家详细介绍一下吧... 目录什么是python虚拟环境一、使用venv创建和管理虚拟环境1.1 创建虚拟环境1.2 激活虚拟环境1.3 验证虚

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

Java List 使用举例(从入门到精通)

《JavaList使用举例(从入门到精通)》本文系统讲解JavaList,涵盖基础概念、核心特性、常用实现(如ArrayList、LinkedList)及性能对比,介绍创建、操作、遍历方法,结合实... 目录一、List 基础概念1.1 什么是 List?1.2 List 的核心特性1.3 List 家族成

c++日志库log4cplus快速入门小结

《c++日志库log4cplus快速入门小结》文章浏览阅读1.1w次,点赞9次,收藏44次。本文介绍Log4cplus,一种适用于C++的线程安全日志记录API,提供灵活的日志管理和配置控制。文章涵盖... 目录简介日志等级配置文件使用关于初始化使用示例总结参考资料简介log4j 用于Java,log4c

史上最全MybatisPlus从入门到精通

《史上最全MybatisPlus从入门到精通》MyBatis-Plus是MyBatis增强工具,简化开发并提升效率,支持自动映射表名/字段与实体类,提供条件构造器、多种查询方式(等值/范围/模糊/分页... 目录1.简介2.基础篇2.1.通用mapper接口操作2.2.通用service接口操作3.进阶篇3

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Python实现Word转PDF全攻略(从入门到实战)

《Python实现Word转PDF全攻略(从入门到实战)》在数字化办公场景中,Word文档的跨平台兼容性始终是个难题,而PDF格式凭借所见即所得的特性,已成为文档分发和归档的标准格式,下面小编就来和大... 目录一、为什么需要python处理Word转PDF?二、主流转换方案对比三、五套实战方案详解方案1:

Spring WebClient从入门到精通

《SpringWebClient从入门到精通》本文详解SpringWebClient非阻塞响应式特性及优势,涵盖核心API、实战应用与性能优化,对比RestTemplate,为微服务通信提供高效解决... 目录一、WebClient 概述1.1 为什么选择 WebClient?1.2 WebClient 与

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.