阅读《Android 从入门到精通》(10)——单项选择
来源:互联网 发布:网络挂号:厦门长庚医院 编辑:程序博客网 时间:2024/05/16 08:45
单项选择(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" > <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>
0 0
- 阅读《Android 从入门到精通》(10)——单项选择
- 阅读《Android 从入门到精通》(9)——多项选择
- 阅读《Android 从入门到精通》(1)——了解 Android 开发
- 阅读《Android 从入门到精通》(2)——熟悉开发环境
- 阅读《Android 从入门到精通》(3)——Activity 与界面
- 阅读《Android 从入门到精通》(4)——人机界面常用组件概述
- 阅读《Android 从入门到精通》(5)——文本框视图
- 阅读《Android 从入门到精通》(?)——View 和 ViewGroup
- 阅读《Android 从入门到精通》(6)——按钮
- 阅读《Android 从入门到精通》(7)——图片按钮
- 阅读《Android 从入门到精通》(8)——编辑框
- 阅读《Android 从入门到精通》(11)——下拉列表
- 阅读《Android 从入门到精通》(12)——自动完成文本框
- 阅读《Android 从入门到精通》(13)——日期选择器
- 阅读《Android 从入门到精通》(14)——时间选择器
- 阅读《Android 从入门到精通》(15)——数字时钟
- 阅读《Android 从入门到精通》(16)——表状时钟
- 阅读《Android 从入门到精通》(17)——进度条
- iOS之UIGestureRecognizer_手势识别的简单用法
- DBUtils
- java中Object... 的用法是什么意思?
- 徐家骏:我在华为工作十年的感悟(转载)
- 并行原理分析(libcurl+epoll)
- 阅读《Android 从入门到精通》(10)——单项选择
- 全球化视野与创新
- 网管人员必备的命令
- [提醒]曾经犯的谜之错误
- java+python+YuiCompressor实现多css js的合并与压缩
- Android手机怎样导出数据库文件
- 关于初学者上传文件到github的方法
- 《人工智能学家》启动2016世界人工智能智商测试,原理和方法
- [leetcode] 15. 3Sum