休言女子非英物,夜夜龙泉壁上鸣。这篇文章主要讲述阅读《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【阅读《Android 从入门到精通》(10)——单项选择】
< 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>
推荐阅读
- macAndroid Studio 真机测试 配置
- [FMX]将 Android 程序切换到后台及从后台切换到前台实现
- win7小马激活工具,本文教您小马激活工具激活win7
- 笔记本无线网卡,本文教您惠普笔记本无线网卡驱动
- 笔记本硬件,本文教您笔记本硬件检测工具
- 笔记本关机关不了,本文教您笔记本关机关不了怎样处理
- qq聊天记录在啥地方个文件夹,本文教您打开qq聊天记录保存文件夹
- 笔记本屏幕变暗,本文教您笔记本屏幕变暗怎样办
- 删除程序?mac,本文教您MAC系统怎样删除软件