ActivityGroup解决TabHost中多个Activity跳转

原文地址 http://www.apkbus.com/android-45401-1-1.html

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


 



两个选项卡的实现


   布局文件  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.         <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
  6.                 android:id="@android:id/tabhost" android:layout_width="fill_parent"
  7.                 android:layout_height="fill_parent">
  8.                 <LinearLayout android:orientation="vertical"
  9.                         android:layout_width="fill_parent" android:layout_height="fill_parent">

  10.                         <TabWidget android:id="@android:id/tabs"
  11.                                 android:layout_width="fill_parent" android:layout_height="wrap_content" />
  12.                         <FrameLayout android:id="@android:id/tabcontent"
  13.                                 android:layout_width="fill_parent" android:layout_height="wrap_content"
  14.                                 android:layout_weight="3">

  15.                         </FrameLayout>


  16.                 </LinearLayout>
  17.         </TabHost>

  18. </LinearLayout>
复制代码

Java代码实现  MainActivity.java


Java代码
  1. package hkp.test;

  2. import android.app.TabActivity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.widget.TabHost;

  6. public class MainActivity extends TabActivity {

  7.         private  TabHost tabHost;
  8.         
  9.         @Override
  10.         protected void onCreate(Bundle savedInstanceState) {
  11.                 // TODO Auto-generated method stub
  12.                 super.onCreate(savedInstanceState);
  13.                 setContentView(R.layout.main);
  14.                 
  15.                 tabHost = getTabHost();
  16.                 
  17.                 tabHost.addTab(tabHost.newTabSpec("tab1")
  18.                                 .setIndicator("First")
  19.                         .setContent(new Intent(this,FirstGroupTab.class)));//第一个选项卡使用一个ActivityGroup
  20.                 tabHost.addTab(tabHost.newTabSpec("tab2")
  21.                                 .setIndicator("Second")
  22.                         .setContent(new Intent(this, SecondTab.class)));//第二个选项卡是一个Activity

  23.                 tabHost.setCurrentTab(0);
  24.         }

  25. }
复制代码
使用 ActivityGroup的管理

Java代码
  1. package hkp.test;

  2. import android.app.ActivityGroup;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.Window;

  7. /**
  8. * @author HuangKaipeng hkp2006@gmail.com
  9. * 2011-10-5
  10. *
  11. */
  12. public class FirstGroupTab extends ActivityGroup {
  13.         
  14.         /**
  15.          * 一个静态的ActivityGroup变量,用于管理本Group中的Activity
  16.          */
  17.         public static ActivityGroup group;
  18.         
  19.         @Override
  20.         protected void onCreate(Bundle savedInstanceState) {
  21.                 // TODO Auto-generated method stub
  22.                 super.onCreate(savedInstanceState);
  23.                 
  24.                 group = this;
  25.                 
  26.         }

  27.         @Override
  28.         public void onBackPressed() {
  29.                 // TODO Auto-generated method stub
  30. //                super.onBackPressed();
  31.                 //把后退事件交给子Activity处理
  32.                 group.getLocalActivityManager()
  33.                         .getCurrentActivity().onBackPressed();
  34.         }

  35.         @Override
  36.         protected void onResume() {
  37.                 // TODO Auto-generated method stub
  38.                 super.onResume();
  39.                 //把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
  40.                 //调用搞得是onResume方法
  41.                 
  42.                 //要跳转的界面
  43.                 Intent intent = new Intent(this, FirstActivity.class).
  44.                       addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  45.                 //把一个Activity转换成一个View
  46.                 Window w = group.getLocalActivityManager().startActivity("FirstActivity",intent);
  47.             View view = w.getDecorView();
  48.             //把View添加大ActivityGroup中
  49.             group.setContentView(view);
  50.         }
复制代码
  ActivityGroup中的第一个Activity


Java代码
  1. package hkp.test;

  2. import android.app.Activity;
  3. import android.content.Intent;
  4. import android.os.Bundle;
  5. import android.view.View;
  6. import android.view.Window;
  7. import android.view.View.OnClickListener;
  8. import android.widget.Button;

  9. /**
  10. * @author HuangKaipeng hkp2006@gmail.com
  11. * 2011-10-5
  12. *
  13. */
  14. public class FirstActivity extends Activity {

  15.         @Override
  16.         protected void onCreate(Bundle savedInstanceState) {
  17.                 // TODO Auto-generated method stub
  18.                 super.onCreate(savedInstanceState);
  19.                 setContentView(R.layout.first_activity);
  20.                 
  21.                 //跳转到第二个界面
  22.                 Button btn = (Button) findViewById(R.id.btn);
  23.                 btn.setOnClickListener(new OnClickListener() {
  24.                         
  25.                         @Override
  26.                         public void onClick(View v) {
  27.                                 // TODO Auto-generated method stub
  28.                                 Intent intent = new Intent(FirstActivity.this, SecondActivity.class).
  29.                                       addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  30.                                 //把一个Activity转换成一个View
  31.                                 Window w = FirstGroupTab.group.getLocalActivityManager()
  32.                                                 .startActivity("SecondActivity",intent);
  33.                             View view = w.getDecorView();
  34.                             //把View添加大ActivityGroup中
  35.                             FirstGroupTab.group.setContentView(view);
  36.                         }
  37.                 });
  38.         }

  39. }
复制代码
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
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值