阅读《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 ...
随机推荐
- 微软ASP.NET网站部署指南(3):使用Web.Config文件的Transformations
1. 综述 大多数程序里都会在Web.config里设置參数,而且在部署的时候须要更改. 每次都手工更改这些配置非常乏味,也easy出错. 该章节将会告诉你假设通过自己主动化更新Web.config文 ...
- unity3d教程运行物理机制
首先,我们将把Hooke定律写Euler方法结合在一起找到新坐标.加速和速度. Hooke定律是F=kx,这里的F是指由水流产生的力(记住,我们将把水体表面模拟为水流),k是指水流的常量.x则是位移. ...
- Bash脚本中的操作符
一.文件測试操作符 假设以下的条件成立将会返回真. -e 文件存在 -a 文件存在 这个选项的效果与-e同样. 可是它已经被"弃用"了, 而且不鼓舞使用. -f 表示这个文件是一个 ...
- zzulioj--1858--单词翻转(模拟)
1858: 单词翻转 Time Limit: 1 Sec Memory Limit: 128 MB Submit: 88 Solved: 35 SubmitStatusWeb Board Desc ...
- maven冲突管理及依赖管理实践
1.“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突 Maven采用“最近获胜策略(nearest wins strategy)”的方式处理依赖冲突,即如果一个项目最终 ...
- 主流的Python领域和框架--转
原文地址:https://www.zhihu.com/question/19899608
- 欢迎来到SQL学院
给学习SQL的同学的福利@ http://sqlschool.modeanalytics.com/ 第一部分 学习SQL 本教程是专为那些想用数据回答问题的人们而设计的.从很大程度上讲,SQL是数据分 ...
- 复杂一些的SQL语句
表连接查询得到结果集后添加数据 INSERT INTO #saleSplitProduct(saleorderID,ProductCode,ProductNum,ProductPrice, produ ...
- python写的爬虫工具,抓取行政村的信息并写入到hbase里
python的版本是2.7.10,使用了两个第三方模块bs4和happybase,可以通过pip直接安装. 1.logger利用python自带的logging模块配置了一个简单的日志输出 2.get ...
- swift使用查阅资料备份1
SnapKit RxSwift R.swift https://www.jianshu.com/p/68e12b966d86 iOS - RxSwift 项目实战记录 https://blog.csd ...