Android组件的使用:RadioButton  新建一个Android项目basiccomponent2 4、把RadioButtonActivity配置到AndroidManifest.xml文件中,并设置程序入口,代码如下

1、新建一个布局文件radiobutton_layout.xml,代码如下:

 1 <?xml version="1.0" encoding="utf-8"?>
 2 <LinearLayout       --> 定义线性布局管理器                         
 3     xmlns:andro>-->引用命名空间
 4     android:layout_width="match_parent"                        -->设置父容器的宽度为屏幕宽度
 5     android:layout_height="match_parent"                       -->设置父容器的高度为屏幕高度
 6     android:orientation="vertical" >                           -->设置父容器中组件排列方式为垂直
 7     
 8     <!-- RadioButton组件必须包裹在RadioGroup组件中 -->
 9     <RadioGroup                                         -->定义RadioGroup容器组件
10         android:>设置容器id    
11         android:layout_width="match_parent"             -->设置容器宽度为父容器宽度    
12         android:layout_height="wrap_content"            -->设置容器高度为包裹内容高度
13        android:orientation="horizontal">                -->设置容器中组件排列方式为水平
14 <!-- 设置RadioButton组件 --> 15 <RadioButton -->定义RadioButton组件 16 android:>-->设置组件id 17 android:layout_width="wrap_content" -->设置组件宽度为包裹内容宽度 18 android:layout_height="wrap_content" -->设置组件高度为包裹内容高度 19 android:text="@string/rb_sex_male_txt"/> -->设置组件默认文本,从string.xml中读取id为rb_sex_male_txt的内容
20 <!-- 设置RadioButton组件 --> 21 <RadioButton 22 android:>-->设置组件id 23 android:layout_width="wrap_content" -->设置组件宽度为包裹内容宽度 24 android:layout_height="wrap_content" -->设置组件高度为包裹内容高度 25 android:text="@string/rb_sex_female_txt"/> -->设置组件默认文本,从string.xml中读取id为rb_sex_female_txt的内容 27 </RadioGroup> 28 </LinearLayout>

2、在string.xml中定义两个文本,代码如下:

1 <?xml version="1.0" encoding="utf-8"?>
2 <resources>
3     <string name="app_name">BasicComponent2</string>   -->应用名称
4     <string name="action_settings">Settings</string>   
5     <!-- 定义文本内容 -->
6     <string name="rb_sex_male_txt">男</string>         -->设置文本
7     <string name="rb_sex_female_txt">女</string>       -->设置文本
8 </resources>

3、新建一个RadioButtonActivity类继承Activity类,并覆写相应方法:

 1 package com.example.basiccomponent2;
 2 
 3 import android.app.Activity;
 4 import android.os.Bundle;
 5 
 6 public class RadioButtonActivity extends Activity {
 7     //覆写父类方法
 8     protected void onCreate(Bundle savedInstanceState) {
 9         super.onCreate(savedInstanceState);                   //调用父类oncreate()方法
10         super.setContentView(R.layout.radiobutton_layout);    //设置布局管理器
11     }
12 }

4、把RadioButtonActivity配置到AndroidManifest.xml文件中,并设置程序入口,代码如下

1 <activity
2             android:name="com.example.basiccomponent2.RadioButtonActivity"   -->Activity类,格式为:包名+类名 或者为:.类名
3             android:label="@string/app_name" >        -->应用名
4             <intent-filter>                  -->设置程序主入口,相当于java中的main()方法,xml文件中只能有一个此标签
5                 <action android:name="android.intent.action.MAIN" />
6                 <category android:name="android.intent.category.LAUNCHER" />
7             </intent-filter>
8</activity>

2013-07-19