单项选择(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)——单项选择的更多相关文章

  1. Android从入门到精通pdf+书源代码

    不须要积分,免费放送 Android从入门到精通的pdf,入门的好书籍,因为csdn文件大小的限制所以分成了两部分. part1地址:http://download.csdn.net/detail/a ...

  2. Android Volley入门到精通:使用Volley加载网络图片

    在上一篇文章中,我们了解了Volley到底是什么,以及它的基本用法.本篇文章中我们即将学习关于Volley更加高级的用法,如何你还没有看过我的上一篇文章的话,建议先去阅读Android Volley完 ...

  3. 阅读《Android 从入门到精通》(12)——自己主动完毕文本框

    自己主动完毕文本框(AutoCompleteTextView) java.lang.Object; android.view.View; android.view.TextView; android. ...

  4. 阅读《Android 从入门到精通》(9)——多项选择

    多项选择(CheckBox) CheckBox 类是 Button 的子类,层次关系例如以下: android.widget.Button android.widget.CompoundButton ...

  5. 阅读《Android 从入门到精通》(17)——进度条

    进度条(ProgressBar) java.lang.Object; android.view.View; android.widget.ProgressBar; ProgressBar 类方法 Pr ...

  6. 阅读《Android 从入门到精通》(24)——切换图片

    切换图片(ImageSwitcher) java.lang.Object; android.view.View; android.widget.ViewGroup; android.widget.Fr ...

  7. 阅读《Android 从入门到精通》(33)——Intent 分类

    Intent 分类 显式 Intent:Intent("android.intent.action.CALL", Uri.parse("tel:" + stri ...

  8. 阅读《Android 从入门到精通》(15)——数字时钟

    数字时钟(DigitalClock) java.lang.Object; android.view.View; android.widget.TextView; android.widget.Digi ...

  9. 阅读《Android 从入门到精通》(29)——四大布局

    LinearLayout 类方法 watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQ ...

随机推荐

  1. linux网络启动报错

    报错信息: shutting down interface eth0: error:device "eth0" (/org/freedsktop/networkMaager/Dev ...

  2. 0x29 总结与练习

    搜索真的菜..困扰了很久,上个星期天没休息好导致整个礼拜没有精神.. 大概完成得七七八八了吧.真是深切的体会到暴力出奇迹的疯狂啊. 3.虫食算 从末位开始枚举判断,通过加数可以推出和的字母代表的数.那 ...

  3. php面向对象之构造函数和析构函数

    php面向对象之构造函数和析构函数 简介 php面向对象支持两种形式的构造函数和析构函数,一种是和类同名的构造函数(php5.5之前),一类是魔术方法(php5.5之后).与类名相同的构造函数优先级比 ...

  4. ubuntu16.04下配置caffe无GPU

    1. 安装依赖项  1 sudo apt-get install libprotobuf-dev libleveldb-dev libsnappy-dev libopencv-dev libhdf5- ...

  5. Oracle 数据库勒索病毒 RushQL 处理办法

    处理办法来自Oracle 官方: https://blogs.oracle.com/cnsupport_news/%E5%AF%B9%E6%95%B0%E6%8D%AE%E5%BA%93%E7%9A% ...

  6. POJ 3150 循环矩阵的应用

    思路: 首先 先普及一个性质: 循环矩阵*循环矩阵=循环矩阵 由于此题是距离小于d的都加上一个数. 那么 构造矩阵的时候 我们发现 诶呦 这是个循环矩阵 看看数据范围 n^2log(k)可以过. 那就 ...

  7. 图片词典 Picture Dictionary

    图片词典/可视词典 Picture Dictionary 某些 APP 又有新功能可以加入了.

  8. T7315 yyy矩阵折叠(长)

    题目背景 全场基本暴零 题目描述 输入输出格式 输入格式: 如图 输出格式: 如图 输入输出样例 输入样例#1: 2 2 1 -2 3 -4 输出样例#1: 4 输入样例#2: 2 5 1 -2 -3 ...

  9. table-layout:fixed属性

    说实话,第一次见到这个属性有点懵圈,真是没见过这个属性 好了,直接说作用 table-layout其实是一种表格算法,用来显示表格单元格.行.列的算法规则. 固定表格布局: 固定表格布局与自动表格布局 ...

  10. 自定义view 之多个引导层动画效果

    SupernatantView 如果我英文还可以的话这个应该叫做漂浮在上层的view---引导层 今天闲来无事看了网上的一些引导层案例总感觉如果不是很舒服,就是类似于很死板的显示和消失 我在想能不能弄 ...