閱讀393 返回首頁    go 阿裏雲 go 技術社區[雲棲]


android的RadioGroup講解

這個主要是如何替換fragment的demo。效果圖如下(下麵的tabhost和上麵的bar不屬於這次的內容,這個是我做的一個應用程序框架的一部分,有需要的或者想研究研究的可以私下聯係),主要是講解中間的內容怎麼實現,即點擊上麵的RadioGroup,下麵的內容一起改變(改變的是XML中的布局,這樣的話下麵三個的布局完全可以自己定義)



1.首先在主界麵的xml中添加一個RadioGroup,裏麵添加三個RadioButton即可

  1. <RadioGroup  
  2.                 android:id="@+id/radioGroup1"  
  3.                 style="@style/layout_full"  
  4.                 android:layout_margin="5dp"  
  5.                 android:background="@drawable/rounded_edittext"  
  6.                 android:orientation="horizontal"  
  7.                 android:padding="5dp" >  
  8.   
  9.                 <RadioButton  
  10.                     android:id="@+id/radio0"  
  11.                     style="@style/layout_horizontal"  
  12.                     android:layout_gravity="center_horizontal"  
  13.                     android:layout_weight="1"  
  14.                     android:checked="true"  
  15.                     android:text="均分" />  
  16.   
  17.                 <RadioButton  
  18.                     android:id="@+id/radio1"  
  19.                     style="@style/layout_horizontal"  
  20.                     android:layout_gravity="center_horizontal"  
  21.                     android:layout_weight="1"  
  22.                     android:text="個人" />  
  23.   
  24.                 <RadioButton  
  25.                     android:id="@+id/radio2"  
  26.                     style="@style/layout_horizontal"  
  27.                     android:layout_gravity="center"  
  28.                     android:layout_weight="1"  
  29.                     android:text="借貸" />  
  30.             </RadioGroup>  

其中
  1. android:background="@drawable/rounded_edittext"  

這一句是給這個RadioGroup添加一個帶圓角的邊框 

rounded_edittext.xml的代碼如下

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <shape xmlns:android="https://schemas.android.com/apk/res/android"  
  3.     android:shape="rectangle" >  
  4.   
  5.     <solid android:color="#ffffff" />  
  6.   
  7.     <corners android:radius="7dip" />  
  8.   
  9.     <stroke  
  10.         android:width="2px"  
  11.         android:color="#000000" />  
  12.   
  13. </shape>  

放置在drawable文件夾下即可


2.下麵的內容由三個xml定義好的布局來呈現,這三個xml的布局可以自己來寫 ,我就很簡單地建了三個,做例子用

speeddial_fragment_pay1.xml

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:layout_margin="5dp"  
  6.     android:background="@drawable/rounded_edittext"  
  7.     android:orientation="vertical" >  
  8.   
  9.     <Button  
  10.         android:id="@+id/Button04"  
  11.         android:layout_width="wrap_content"  
  12.         android:layout_height="wrap_content"  
  13.         android:layout_centerHorizontal="true"  
  14.         android:layout_centerVertical="true"  
  15.         android:text="Button" />  
  16.   
  17. </RelativeLayout>  

3.(重要)在主布局文件中添加Fragment的載體,比如一個framlayout,負責承載fragment

在上麵的RadioGroup的布局下增加:

  1. <FrameLayout  
  2.             android:id="@+id/fragment_container"  
  3.             android:layout_width="match_parent"  
  4.             android:layout_height="match_parent" />  

這樣布局就完成了

4.由於Fragment的特性,我們要新建三個自己的Fragment,都繼承自Fragment 

SpeeddialFragmentOne.java

  1. package com.gracker.fragment;  
  2.   
  3. import android.app.Fragment;  
  4. import android.os.Bundle;  
  5. import android.view.LayoutInflater;  
  6. import android.view.View;  
  7. import android.view.ViewGroup;  
  8.   
  9. import com.gracker.tabactivity.R;  
  10.   
  11. public class SpeeddialFragmentOne extends Fragment {  
  12.   
  13.   
  14.     @Override  
  15.     public void onCreate(Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         super.onCreate(savedInstanceState);  
  18.     }  
  19.   
  20.       
  21.     @Override  
  22.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  23.             Bundle savedInstanceState) {  
  24.         // TODO Auto-generated method stub  
  25.         return inflater.inflate(R.layout.speeddial_fragment_pay1, container, false);  
  26.     }  
  27. }  

這個Fragment非常簡單,沒有添加任何的邏輯,僅僅隻是在onCreateView的要布局然後以View返回。Fragment有很多方法,可以根據自己的需要進行重載,這裏就不多說了,自己到用的時候自然就知道了。

類似地,建立另外兩個Fragment ,改變的僅僅是

  1. return inflater.inflate(R.layout.speeddial_fragment_pay1, container, false);  

5.在主Activity中調用:

MainActivity.java

  1. /** 
  2.  * 主Activity 
  3.  *  
  4.  * @author Gracker Gao 
  5.  * @date 2012.8.15 
  6.  */  
  7. package com.gracker.hostactivity;  
  8.   
  9. import android.app.Activity;  
  10. import android.app.FragmentTransaction;  
  11. import android.os.Bundle;  
  12. import android.util.Log;  
  13. import android.widget.RadioGroup;  
  14. import android.widget.RadioGroup.OnCheckedChangeListener;  
  15.   
  16. import com.gracker.fragment.SpeeddialFragmentOne;  
  17. import com.gracker.fragment.SpeeddialFragmentThree;  
  18. import com.gracker.fragment.SpeeddialFragmentTwo;  
  19. import com.gracker.tabactivity.R;  
  20.   
  21. public class MainActivity extends Activity {  
  22.   
  23.     private final String TAG = "SpeedDialActivity";  
  24.   
  25.     private RadioGroup mRadioGroup;  
  26.     private SpeeddialFragmentTwo mSpeeddialFragmentTwo;  
  27.     private SpeeddialFragmentOne mSpeeddialFragmentOne;  
  28.     private SpeeddialFragmentThree mSpeeddialFragmentThree;  
  29.       
  30.     private FragmentTransaction transaction;  
  31.   
  32.     public void onCreate(Bundle savedInstanceState) {  
  33.         super.onCreate(savedInstanceState);  
  34.         setContentView(R.layout.speeddial);  
  35.           
  36.         init_date();  
  37.         setupWidgets();  
  38.     }  
  39.       
  40.     private void init_date(){  
  41.         transaction = getFragmentManager()  
  42.                 .beginTransaction();  
  43.         if (null == mSpeeddialFragmentOne) {  
  44.             mSpeeddialFragmentOne = new SpeeddialFragmentOne();  
  45.         }  
  46.         transaction.add(R.id.fragment_container,  
  47.                 mSpeeddialFragmentOne);  
  48.         // Commit the transaction  
  49.         transaction.commit();  
  50.     }  
  51.   
  52.     private void setupWidgets() {  
  53.   
  54.         mRadioGroup = (RadioGroup) findViewById(R.id.radioGroup1);  
  55.         mRadioGroup.setOnCheckedChangeListener(new OnCheckedChangeListener() {  
  56.   
  57.             @Override  
  58.             public void onCheckedChanged(RadioGroup group, int checkedId) {  
  59.                 // TODO Auto-generated method stub  
  60.   
  61.                 switch (checkedId) {  
  62.                 case R.id.radio0:  
  63.                     Log.v(TAG, "setupWidgets():radio0 clicked");  
  64.                     if (null == mSpeeddialFragmentOne) {  
  65.                         mSpeeddialFragmentOne = new SpeeddialFragmentOne();  
  66.                     }  
  67.                     transaction = getFragmentManager()  
  68.                             .beginTransaction();  
  69.                     transaction.replace(R.id.fragment_container,  
  70.                             mSpeeddialFragmentOne);               
  71.                     // Commit the transaction  
  72.                     transaction.commit();  
  73.                     break;  
  74.                 case R.id.radio1:  
  75.                     Log.v(TAG, "setupWidgets():radio1 clicked");  
  76.                     if (null == mSpeeddialFragmentTwo) {  
  77.                         mSpeeddialFragmentTwo = new SpeeddialFragmentTwo();  
  78.                     }  
  79.                     transaction = getFragmentManager()  
  80.                             .beginTransaction();  
  81.                     transaction.replace(R.id.fragment_container,  
  82.                             mSpeeddialFragmentTwo);                   
  83.                     // Commit the transaction  
  84.                     transaction.commit();  
  85.                     break;  
  86.                 case R.id.radio2:  
  87.                     Log.v(TAG, "setupWidgets():radio2 clicked");  
  88.   
  89.                     if (null == mSpeeddialFragmentThree) {  
  90.                         mSpeeddialFragmentThree = new SpeeddialFragmentThree();  
  91.                     }  
  92.                     transaction = getFragmentManager()  
  93.                             .beginTransaction();  
  94.                     transaction.replace(R.id.fragment_container,  
  95.                             mSpeeddialFragmentThree);                     
  96.                     // Commit the transaction  
  97.                     transaction.commit();  
  98.                     break;  
  99.   
  100.                 default:  
  101.                     break;  
  102.                 }  
  103.             }  
  104.         });  
  105.     }  
  106.   
  107.     @Override  
  108.     protected void onResume() {  
  109.         // TODO Auto-generated method stub  
  110.         super.onResume();  
  111.   
  112.     }  
  113.   
  114.     @Override  
  115.     protected void onDestroy() {  
  116.         // TODO Auto-generated method stub  
  117.         super.onDestroy();  
  118.         // dataEncapsulation.closeDataBase_speedDial();  
  119.     }  
  120.   
  121. }  


init_data()函數中主要是初始化值,包括初始化用戶第一個看到的Fragment

在RadioGroup的onCheckedChangeLinsteer中,切換Fragment。關於Fragment的一些操作,比如增加,刪除,替換等等,可以參照這個帖子:https://www.eoeandroid.com/thread-71642-1-1.html 講的很詳細,我也不想重複。


這個Demo就不提供下載了,畢竟不是很難,所有的東西都交代了,自己敲一遍收獲總是比打開別人的代碼來研究要好的多。

例子中有什麼錯誤的地方歡迎指正。

最後更新:2017-04-03 12:53:35

  上一篇:go 保存手寫簽名
  下一篇:go 手機衛士15-歸屬地界麵和手機定位功能