单选按钮

1. 单选按钮(RadioButton)的基本概念

2. RadioGroup和RadioButton

3. OnClickedListener和OnClickedChangeListener监听器

 

1. 单选按钮(RadioButton)的基本概念

        一组单选按钮只能被选中一个

     

 

2. RadioGroup和RadioButton

        <TextView   />                                 表示既是开始又是结束

        <RadioGroup>   </RadioGroup>        里面可以增加子标签

    fragment_main.xml

 1 <LinearLayout 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     android:orientation="vertical"
 6     android:paddingBottom="@dimen/activity_vertical_margin"
 7     android:paddingLeft="@dimen/activity_horizontal_margin"
 8     android:paddingRight="@dimen/activity_horizontal_margin"
 9     android:paddingTop="@dimen/activity_vertical_margin"
10     tools:context="first.pack.MainActivity$PlaceholderFragment" >
11 
12     <RadioGroup
13         android:id="@+id/radioGroup"
14         android:layout_width="wrap_content"
15         android:layout_height="wrap_content"
16         android:orientation="horizontal">
17         
18         <RadioButton
19             android:id="@+id/femaleButtonId"
20             android:layout_width="wrap_content"
21             android:layout_height="wrap_content"
22             android:text="female"/>
23         
24         <RadioButton
25             android:id="@+id/maleButtonId"
26             android:layout_width="wrap_content"
27             android:layout_height="wrap_content"
28             android:text="male"/>
29     </RadioGroup>
30 
31 </LinearLayout>

      MainActivity.java

 1 public static class PlaceholderFragment extends Fragment {
 2 
 3         private RadioGroup radioGroup;
 4         private RadioButton femaleButton;
 5         private RadioButton maleButton;
 6         
 7         public PlaceholderFragment() {
 8         }
 9 
10         @Override
11         public View onCreateView(LayoutInflater inflater, ViewGroup container,
12                 Bundle savedInstanceState) {
13             View rootView = inflater.inflate(R.layout.fragment_main, container, false);
14             
15             radioGroup = (RadioGroup)rootView.findViewById(R.id.radioGroup);
16             femaleButton = (RadioButton)rootView.findViewById(R.id.femaleButtonId);
17             maleButton = (RadioButton)rootView.findViewById(R.id.maleButtonId);
18             
19             RadioGroupListener listener = new RadioGroupListener();
20             radioGroup.setOnCheckedChangeListener(listener);
21             
22             return rootView;
23         }
24         
25         class RadioGroupListener implements OnCheckedChangeListener{
26 
27             @Override
28             public void onCheckedChanged(RadioGroup group, int checkedId) {
29                 // TODO Auto-generated method stub
30                 if(checkedId == femaleButton.getId()){
31                     Log.i("tag","选中了female");
32                 }
33                 else if(checkedId == maleButton.getId()){
34                     Log.i("tag","选中了male");
35                 }
36             }           
37         }
38     }   

     分别选中female和male, 一个group里面只能选中一个

    

     输出为:

        

 

 

posted @ 2014-06-08 00:58  Mirrorhanman  阅读(503)  评论(0编辑  收藏  举报