1.5 Activity08 RadioButton

 

1.RadioButton是多选一的按钮,多个RadioButton放在一个RadioGroup中,在一个RadioGroup中,用户只能选择一个RadioButton

2.布局文件

 

activity_main.xml
 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <RadioGroup 
 7         android:id="@+id/genderGroup"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:orientation="vertical"
11         >
12         <RadioButton 
13             android:id="@+id/femaleButton"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:text="@string/femaleButton">
17             
18         </RadioButton>    
19         <RadioButton 
20             android:id="@+id/maleButton"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="@string/maleButton"     ></RadioButton>
24      </RadioGroup>
25             </RelativeLayout>

 

3.设置监听器

1  genterGroup.setOnCheckedChangeListener(new RadioGroupListener());
View Code
 1  class RadioGroupListener implements OnCheckedChangeListener{
 2 
 3             public void onCheckedChanged(RadioGroup arg0, int arg1) {
 4                 // TODO Auto-generated method stub
 5                 if(femaleButton.getId()==arg1){
 6                     System.out.println("female");
 7                 }
 8                 if(maleButton.getId()==arg1){
 9                     System.out.println("male");
10                     
11                 }
12             }
13             
14         }

4.注意,监听器函数不在onCreate函数中。

 

 1 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
 2     xmlns:tools="http://schemas.android.com/tools"
 3     android:layout_width="match_parent"
 4     android:layout_height="match_parent" >
 5 
 6     <RadioGroup 
 7         android:id="@+id/genderGroup"
 8         android:layout_width="wrap_content"
 9         android:layout_height="wrap_content"
10         android:orientation="vertical"
11         >
12         <RadioButton 
13             android:id="@+id/femaleButton"
14             android:layout_width="wrap_content"
15             android:layout_height="wrap_content"
16             android:text="@string/femaleButton">
17             
18         </RadioButton>    
19         <RadioButton 
20             android:id="@+id/maleButton"
21             android:layout_width="wrap_content"
22             android:layout_height="wrap_content"
23             android:text="@string/maleButton"     ></RadioButton>
24      </RadioGroup>
25             </RelativeLayout>

 

 

 

 

 

 

 

posted @ 2012-10-24 00:18  亘大  阅读(164)  评论(0)    收藏  举报