zoukankan      html  css  js  c++  java
  • 用ActivityGroup解决TabHost中多个Activity跳转问题


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

     

      

     

     

    两个选项卡的实现

     

       布局文件  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>  
  • 相关阅读:
    Ubuntu解压缩命令
    小语种优化策略
    外贸seo常用Zen Cart数据库mysql批量执行命令
    ASP Request.ServerVariables
    centos linux 下 crontab e 命令插入及保存
    AJAX 传值给后台
    partial class
    Jquery 实现弹出层
    abstract class
    SQL锁表
  • 原文地址:https://www.cnblogs.com/javawebsoa/p/3020122.html
Copyright © 2011-2022 走看看