代码、收藏-用ActivityGroup解决TabHost中多个Activity跳转问题-by小雨

PS:今天上午,非常郁闷,有很多简单基础的问题搞得我有些迷茫,哎,代码几天不写就忘。目前又不当COO,还是得用心记代码哦!

    

    面下片图是测试程序的效果图

     

      

     

     

    两个选项卡的现实

     

       局布文件  main.xml

     

    

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent">  
  5.   
  6.     <TabHost xmlns:android="http://schemas.android.com/apk/res/android"  
  7.         android:id="@android:id/tabhost" android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent">  
  9.         <LinearLayout android:orientation="vertical"  
  10.             android:layout_width="fill_parent" android:layout_height="fill_parent">  
  11.   
  12.             <TabWidget android:id="@android:id/tabs"  
  13.                 android:layout_width="fill_parent" android:layout_height="wrap_content" />  
  14.             <FrameLayout android:id="@android:id/tabcontent"  
  15.                 android:layout_width="fill_parent" android:layout_height="wrap_content"  
  16.                 android:layout_weight="3">  
  17.   
  18.             </FrameLayout>  
  19.   
  20.   
  21.         </LinearLayout>  
  22.     </TabHost>  
  23.   
  24. </LinearLayout>  

     

     

        Java代码现实  MainActivity.java

     

    

Java代码  收藏代码
  1. package hkp.test;  
  2.   
  3. import android.app.TabActivity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.widget.TabHost;  
  7.   
  8. public class MainActivity extends TabActivity {  
  9.   
  10.     private  TabHost tabHost;  
  11.       
  12.     @Override  
  13.     protected void onCreate(Bundle savedInstanceState) {  
  14.         // TODO Auto-generated method stub  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.           
  18.         tabHost = getTabHost();  
  19.           
  20.         tabHost.addTab(tabHost.newTabSpec("tab1")  
  21.                 .setIndicator("First")  
  22.                 .setContent(new Intent(this,FirstGroupTab.class)));//第一个选项卡应用一个ActivityGroup  
  23.         tabHost.addTab(tabHost.newTabSpec("tab2")  
  24.                 .setIndicator("Second")  
  25.                 .setContent(new Intent(this, SecondTab.class)));//第二个选项卡是一个Activity  
  26.   
  27.         tabHost.setCurrentTab(0);  
  28.     }  
  29.    
  30. }  

     

        应用 ActivityGroup的管理

     

    

Java代码  收藏代码
  1. package hkp.test;  
  2.   
  3. import android.app.ActivityGroup;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.Window;  
  8.   
  9. /** 
  10.  * @author HuangKaipeng hkp2006@gmail.com 
  11.  * 2011-10-5 
  12.  * 
  13.  */  
  14. public class FirstGroupTab extends ActivityGroup {  
  15.       
  16.     /** 
  17.      * 一个静态的ActivityGroup变量,于用管理本Group中的Activity 
  18.      */  
  19.     public static ActivityGroup group;  
  20.       
  21.     @Override  
  22.     protected void onCreate(Bundle savedInstanceState) {  
  23.         // TODO Auto-generated method stub  
  24.         super.onCreate(savedInstanceState);  
  25.           
  26.         group = this;  
  27.           
  28.     }  
  29.   
  30.     @Override  
  31.     public void onBackPressed() {  
  32.         // TODO Auto-generated method stub  
  33. //      super.onBackPressed();  
  34.         //把后退事件交给子Activity处置  
  35.         group.getLocalActivityManager()  
  36.             .getCurrentActivity().onBackPressed();  
  37.     }  
  38.   
  39.     @Override  
  40.     protected void onResume() {  
  41.         // TODO Auto-generated method stub  
  42.         super.onResume();  
  43.         //把面界切换放到onResume方法中是因为,从其他选项卡切换回来时,  
  44.         //调用搞得是onResume方法  
  45.           
  46.         //要转跳的面界  
  47.         Intent intent = new Intent(this, FirstActivity.class).  
  48.                   addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  49.         //把一个Activity转换成一个View  
  50.         Window w = group.getLocalActivityManager().startActivity("FirstActivity",intent);  
  51.         View view = w.getDecorView();  
  52.         //把View添加大ActivityGroup中  
  53.         group.setContentView(view);  
  54.     }  

     

        ActivityGroup中的第一个Activity

     

     

    

Java代码  收藏代码
  1. package hkp.test;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.Window;  
  8. import android.view.View.OnClickListener;  
  9. import android.widget.Button;  
  10.   
  11. /** 
  12.  * @author HuangKaipeng hkp2006@gmail.com 
  13.  * 2011-10-5 
  14.  * 
  15.  */  
  16. public class FirstActivity extends Activity {  
  17.   
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         // TODO Auto-generated method stub  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.first_activity);  
  23.           
  24.         //转跳到第二个面界  
  25.         Button btn = (Button) findViewById(R.id.btn);  
  26.         btn.setOnClickListener(new OnClickListener() {  
  27.               
  28.             @Override  
  29.             public void onClick(View v) {  
  30.                 // TODO Auto-generated method stub  
  31.                 Intent intent = new Intent(FirstActivity.this, SecondActivity.class).  
  32.                           addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);  
  33.                 //把一个Activity转换成一个View  
  34.                 Window w = FirstGroupTab.group.getLocalActivityManager()  
  35.                         .startActivity("SecondActivity",intent);  
  36.                 View view = w.getDecorView();  
  37.                 //把View添加大ActivityGroup中  
  38.                 FirstGroupTab.group.setContentView(view);  
  39.             }  
  40.         });  
  41.     }  
  42.   
  43. }  

     

     

        XML

     

    

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7. <TextView    
  8.     android:layout_width="fill_parent"   
  9.     android:layout_height="wrap_content"   
  10.     android:text="这个是ActivityGroup中的第一个面界!"  
  11.     />  
  12.     <Button android:id="@+id/btn"   
  13.         android:layout_width="fill_parent"  
  14.         android:layout_height="wrap_content"  
  15.         android:text="转跳到本组中的另一个Activity中"/>  
  16. </LinearLayout>  

文章结束给大家分享下程序员的一些笑话语录: 与女友分手两月有余,精神萎靡,面带菜色。家人介绍一女孩,昨日与其相亲。女孩果然漂亮,一向吝啬的我决定破例请她吃晚饭。
选了一个蛮贵的西餐厅,点了比较贵的菜。女孩眉开眼笑,与我谈得很投机。聊着聊着,她说:“我给你讲个笑话吧。”“ok”
  “一只螳螂要给一只雌蝴蝶介绍对象,见面时发现对方是只雄蜘蛛。见面后螳螂问蝴蝶‘如何?’,‘他长的太难看了’,‘别看人家长的丑,人家还有网站呢’。”
  “呵呵………”我笑。忽然她问:“你有网站吗?”  

posted @ 2013-04-14 14:18  坚固66  阅读(249)  评论(0编辑  收藏  举报