阅读《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 ...
随机推荐
- BZOJ4031——HEOI小z的房间
题意:求某网格图生成树个数,对1e9取模 题解:题目是裸的Matrix-Tree定理,这不是我要说的重点,重点是对于这个取模的处理. 由于这不是个质数,所以不能直接乘逆元来当除法用.直接高斯消元肯定是 ...
- 从零单排入门机器学习:线性回归(linear regression)实践篇
线性回归(linear regression)实践篇 之前一段时间在coursera看了Andrew ng的机器学习的课程,感觉还不错,算是入门了. 这次打算以该课程的作业为主线,对机器学习基本知识做 ...
- Oracle学习(11):PLSQL程序设计
PL/SQL程序结构及组成 什么是PL/SQL? •PL/SQL(Procedure Language/SQL) •PLSQL是Oracle对sql语言的过程化扩展 •指在SQL命令语言中添加了过程处 ...
- Educational Codeforces Round 12 E. Beautiful Subarrays trie求两异或值大于等于k对数
E. Beautiful Subarrays One day, ZS the Coder wrote down an array of integers a with elements a1, ...
- Word技巧杂记(一)——去掉页眉上方的黑线
今天在调整文章的格式时,突然发现在页眉的上方有一条巨粗无比的黑线,不知从何处冒出来的(如下图) 经过长时间的研究,终于发现原来这是页面的边框.解决办法也很简单: 格式->边框与底纹->页面 ...
- 0x25 广度优先搜索
今天莫名不想说话. 结果发现效率挺高? poj3322 本来可以1a的..发现我宽搜写的是head<=tail而且初始是head=1,tail=2如果是多组数据简直就gg了.基础不牢固 这题虽然 ...
- PHP魔术方法__clone()篇
PHP中定义了一个内置方法__clone()来调整兑现的克隆行为: 当一个对象被克隆的时候会自动执行__clone()方法,而复制的对象可以在其方法内进行调整 header('Content-type ...
- LeetCode Golang 单向链表相加 反向实现
LeetCode 两数之和, 反向实现 1 -> 2 -> 3 -> 4 + 3 -> 4 ------------------------- ...
- 服务器搭建域控与SQL Server的AlwaysOn环境过程(五)配置异地机房节点
0 引言 注意点1 注意异地节点最好至少有2个AG节点,否则在本地节点进行手动故障转移的时候会出现仲裁警告,提示WSFC集群有脱机危险 在异地节点只有一个的情况下,虽然Windows2012R2有动态 ...
- java的算法实现冒泡
package xutao3;public class test1 { public static void main(String[] args) { int[] arr={12,88,66,55, ...