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

相关文章

从入门到精通MySQL联合查询

《从入门到精通MySQL联合查询》:本文主要介绍从入门到精通MySQL联合查询,本文通过实例代码给大家介绍的非常详细,需要的朋友可以参考下... 目录摘要1. 多表联合查询时mysql内部原理2. 内连接3. 外连接4. 自连接5. 子查询6. 合并查询7. 插入查询结果摘要前面我们学习了数据库设计时要满

从入门到精通C++11 <chrono> 库特性

《从入门到精通C++11<chrono>库特性》chrono库是C++11中一个非常强大和实用的库,它为时间处理提供了丰富的功能和类型安全的接口,通过本文的介绍,我们了解了chrono库的基本概念... 目录一、引言1.1 为什么需要<chrono>库1.2<chrono>库的基本概念二、时间段(Durat

解析C++11 static_assert及与Boost库的关联从入门到精通

《解析C++11static_assert及与Boost库的关联从入门到精通》static_assert是C++中强大的编译时验证工具,它能够在编译阶段拦截不符合预期的类型或值,增强代码的健壮性,通... 目录一、背景知识:传统断言方法的局限性1.1 assert宏1.2 #error指令1.3 第三方解决

从入门到精通MySQL 数据库索引(实战案例)

《从入门到精通MySQL数据库索引(实战案例)》索引是数据库的目录,提升查询速度,主要类型包括BTree、Hash、全文、空间索引,需根据场景选择,建议用于高频查询、关联字段、排序等,避免重复率高或... 目录一、索引是什么?能干嘛?核心作用:二、索引的 4 种主要类型(附通俗例子)1. BTree 索引(

Redis 配置文件使用建议redis.conf 从入门到实战

《Redis配置文件使用建议redis.conf从入门到实战》Redis配置方式包括配置文件、命令行参数、运行时CONFIG命令,支持动态修改参数及持久化,常用项涉及端口、绑定、内存策略等,版本8... 目录一、Redis.conf 是什么?二、命令行方式传参(适用于测试)三、运行时动态修改配置(不重启服务

Oracle 数据库数据操作如何精通 INSERT, UPDATE, DELETE

《Oracle数据库数据操作如何精通INSERT,UPDATE,DELETE》在Oracle数据库中,对表内数据进行增加、修改和删除操作是通过数据操作语言来完成的,下面给大家介绍Oracle数... 目录思维导图一、插入数据 (INSERT)1.1 插入单行数据,指定所有列的值语法:1.2 插入单行数据,指

Android DataBinding 与 MVVM使用详解

《AndroidDataBinding与MVVM使用详解》本文介绍AndroidDataBinding库,其通过绑定UI组件与数据源实现自动更新,支持双向绑定和逻辑运算,减少模板代码,结合MV... 目录一、DataBinding 核心概念二、配置与基础使用1. 启用 DataBinding 2. 基础布局

Android ViewBinding使用流程

《AndroidViewBinding使用流程》AndroidViewBinding是Jetpack组件,替代findViewById,提供类型安全、空安全和编译时检查,代码简洁且性能优化,相比Da... 目录一、核心概念二、ViewBinding优点三、使用流程1. 启用 ViewBinding (模块级

MySQL DQL从入门到精通

《MySQLDQL从入门到精通》通过DQL,我们可以从数据库中检索出所需的数据,进行各种复杂的数据分析和处理,本文将深入探讨MySQLDQL的各个方面,帮助你全面掌握这一重要技能,感兴趣的朋友跟随小... 目录一、DQL 基础:SELECT 语句入门二、数据过滤:WHERE 子句的使用三、结果排序:ORDE

Android学习总结之Java和kotlin区别超详细分析

《Android学习总结之Java和kotlin区别超详细分析》Java和Kotlin都是用于Android开发的编程语言,它们各自具有独特的特点和优势,:本文主要介绍Android学习总结之Ja... 目录一、空安全机制真题 1:Kotlin 如何解决 Java 的 NullPointerExceptio