阅读《Android 从入门到精通》(10)——单项选择
单项选择(RadioGroup)
RadioGroup 是 LinearLayout 的子类,继承关系例如以下:
android.view.ViewGroup
android.widget.LinearLayout
android.widget.RadioGroup
RadioGroup 类方法
RadioGroup 演示样例
完整project:http://download.csdn.net/detail/sweetloveft/9402088
下述程序中,主要学习 RadioGroup 的使用方法,须要注意单选button的处理逻辑是通过监听 RadioGroup 完毕的,详细处理哪个单选button则是通过 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; @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.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 { @Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
// TODO Auto-generated method stub
for (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" > <TextView
android: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" /> <RadioGroup
android:id="@+id/radioGroup"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="30dp" > <RadioButton
android:id="@+id/radioButton1"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/os1" /> <RadioButton
android:id="@+id/radioButton2"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/os2" /> <RadioButton
android:id="@+id/radioButton3"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/os3" /> <RadioButton
android:id="@+id/radioButton4"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/os4" /> <RadioButton
android:id="@+id/radioButton5"
android:layout_width="160dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/os5" /> <RadioButton
android: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-sdk
android:minSdkVersion="8"
android:targetSdkVersion="19" /> <application
android: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)——单项选择的更多相关文章
- Android从入门到精通pdf+书源代码
不须要积分,免费放送 Android从入门到精通的pdf,入门的好书籍,因为csdn文件大小的限制所以分成了两部分. part1地址:http://download.csdn.net/detail/a ...
- Android Volley入门到精通:使用Volley加载网络图片
在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...
- 阅读《Android 从入门到精通》(12)——自己主动完毕文本框
自己主动完毕文本框(AutoCompleteTextView) java.lang.Object; android.view.View; android.view.TextView; android. ...
- 阅读《Android 从入门到精通》(9)——多项选择
多项选择(CheckBox) CheckBox 类是 Button 的子类,层次关系例如以下: android.widget.Button android.widget.CompoundButton ...
- 阅读《Android 从入门到精通》(17)——进度条
进度条(ProgressBar) java.lang.Object; android.view.View; android.widget.ProgressBar; ProgressBar 类方法 Pr ...
- 阅读《Android 从入门到精通》(24)——切换图片
切换图片(ImageSwitcher) java.lang.Object; android.view.View; android.widget.ViewGroup; android.widget.Fr ...
- 阅读《Android 从入门到精通》(33)——Intent 分类
Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...
- 阅读《Android 从入门到精通》(15)——数字时钟
数字时钟(DigitalClock) java.lang.Object; android.view.View; android.widget.TextView; android.widget.Digi ...
- 阅读《Android 从入门到精通》(29)——四大布局
LinearLayout 类方法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQ ...
随机推荐
- HDU1237 简单计算器 【栈】+【逆波兰式】
简单计算器 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Subm ...
- mysql 免安装配置 数据库修改密码和远程IP登陆
MySQL Windows安装包说明: 1.mysql-5.5.20-win32.msi:Windows 安装包,图形化的下一步下一步的安装. 2.mysql-5.5.20.zip,这个是window ...
- zzulioj--1638--Happy Thanksgiving Day - Say 3Q I(水题)
1638: Happy Thanksgiving Day - Say 3Q I Time Limit: 1 Sec Memory Limit: 128 MB Submit: 91 Solved: ...
- 敏捷开发 —— TDD(测试驱动开发)
测试驱动开发 TDD(Test-Driven Development)是敏捷开发的一项核心实践,同时也是一种设计技术和方法. 既然是测试驱动,便是测试,测试用例先行: 首先编写好测试用例,期待值,实际 ...
- [jzoj 6080] [GDOI2019模拟2019.3.23] IOer 解题报告 (数学构造)
题目链接: https://jzoj.net/senior/#main/show/6080 题目: 题意: 给定$n,m,u,v$ 设$t_i=ui+v$ 求$\sum_{k_1+k_2+...+k_ ...
- 使用sed -i对文本字符串进行增删改查
sed是一个很好的文件处理工具,本身是一个管道命令,主要以行为单位进行处理,可以将数据行进行替换.删除.新增.选取等特定工作.1. sed命令行格式sed [选项] [命令] 1.1 选项-n,使用安 ...
- 循环引用的weak和assgin
__weak 当对象销毁后weakSelf指向的地址为nil __unsafe_unretained 挡圈对象销毁后,weakSelf执行的地址为不变,而地址对应对象已经销毁,再次访问该对象就崩溃,相 ...
- POJ 2446 匈牙利算法
题意: 思路: 二分图匹配... // by SiriusRen #include <cmath> #include <cstdio> #include <cstring ...
- Vue模拟酷狗APP问题总结
一.NewSongs.vue中的38行 this.$http.get('/proxy/?json=true') 里面这个路径的获取 二.router文件夹中的index.js 中的 comp ...
- 详解循环神经网络(Recurrent Neural Network)
本文结构: 模型 训练算法 基于 RNN 的语言模型例子 代码实现 1. 模型 和全连接网络的区别 更细致到向量级的连接图 为什么循环神经网络可以往前看任意多个输入值 循环神经网络种类繁多,今天只看最 ...