用ActivityGroup解决TabHost中多个Activity跳转问题

介绍

有时,我们想在一个window中显示多个视图,这时就需要用到Tab容器。在Android里它叫TabHost。

使用TabHost有两种方式:
(1)在相同的activity中使用TabHost导航多个视图
(2)使用TabHost导航多个Activity(通过intents)
Tab应用的结构
TabHost的Activity的结构如下:

Tabs1.jpg

最近在做一个程序,刚开始没考虑全,就用TabHost做了,后来才发现程序中,需要在一个TabHost内实现多个Activity的跳转,网上搜了一翻,有人建议把TabHost改成Button,然后每个Activity中都处理加入的Button,这样是可以解决问题,但是修改起来很繁琐,所以还是继续寻找替代方法。在网上搜到了《使用ActivityGroup来切换Activity和Layout》一文,但是用在我的程序中还需要有大的改动,所以索性我就自己写了个测试例子,不错,成功了,拿出来和大家分享一下,希望对大家有帮助!

 

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

 

  

 

 

两个选项卡的实现

 

   布局文件  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"//设置tag的Tag
  21.                 .setIndicator("First" //设置tab的标题
  22.                 .setContent(new Intent(this,FirstGroupTab.class)));//设置tag的内容,第一个选项卡使用一个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>  

  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 7
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 7
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值