在一个TabHost内实现多个Activity的跳转

如题所示,需要在一个TabHost内实现实现多个Activity的跳转,下图是TabHost的示例:

1.png

  下面我们使用ActivityGroup类来实现这个程序,关于ActivityGroup类的使用方法,可以查看API。

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

220bc091-8a10-3150-a410-6328d5c80a2a.png

4267b2b1-72ca-365a-ad53-a5b31fef93c7.png

72e68b38-21c5-3cde-aea7-3140a0e68f25.png

布局文件 main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:orientation="vertical" android:layout_width="fill_parent"
        android:layout_height="fill_parent">
   <TabHost xmlns:android="http://schemas.android.com/apk/res/android"
       
         android:id="@android:id/tabhost" 

                android:layout_width="fill_parent"

                android:layout_height="fill_parent">

                <LinearLayout android:orientation="vertical"
                        android:layout_width="fill_parent" 

                            android:layout_height="fill_parent">
                               <TabWidget android:id="@android:id/tabs"
                                android:layout_width="fill_parent"

                               android:layout_height="wrap_content" />
                        <FrameLayout android:id="@android:id/tabcontent"
                                android:layout_width="fill_parent" 

                                android:layout_height="wrap_content"
                                android:layout_weight="3">
                                </FrameLayout>
                      </LinearLayout>
        </TabHost>
</LinearLayout>

首先和,我们还是要创建TabHost来建立选项卡,MainActivity的代码:

import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TabHost;


public class MainActivity extends TabActivity {

       private  TabHost tabHost;
         @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.main);
                
                tabHost = getTabHost();
                
                tabHost.addTab(tabHost.newTabSpec("tab1")
                                .setIndicator("First")
                        .setContent(new Intent(this,FirstGroupTab.class)));//第一个选项卡使用一个ActivityGroup
                tabHost.addTab(tabHost.newTabSpec("tab2")
                                .setIndicator("Second")
                        .setContent(new Intent(this, SecondTab.class)));//第二个选项卡是一个Activity


                tabHost.setCurrentTab(0);
        }
 }

然后就是要在一个选项卡里实现Activity的跳转,使用 ActivityGroup来管理:

import android.app.ActivityGroup;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;


public class FirstGroupTab extends ActivityGroup {
        
      
        public static ActivityGroup group;  一个静态的ActivityGroup变量,用于管理本Group中的Activity
        
        @Override
        protected void onCreate(Bundle savedInstanceState) {
            
                super.onCreate(savedInstanceState);
                
                group = this;
                
        }


        @Override
        public void onBackPressed() {
           
                super.onBackPressed();       //把后退事件交给子Activity处理
         
                group.getLocalActivityManager()
                        .getCurrentActivity().onBackPressed();
        }


        @Override
        protected void onResume() {
           
                super.onResume();
                //把界面切换放到onResume方法中是因为,从其他选项卡切换回来时,
                //调用搞得是onResume方法
                
                //要跳转的界面
                Intent intent = new Intent(this, FirstActivity.class).
                      addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                //把一个Activity转换成一个View
                Window w = group.getLocalActivityManager().startActivity("FirstActivity",intent);
            View view = w.getDecorView();
            //把View添加大ActivityGroup中
            group.setContentView(view);
        }


  ActivityGroup中的第一个Activity:

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.View.OnClickListener;
import android.widget.Button;


public class FirstActivity extends Activity {


        @Override
        protected void onCreate(Bundle savedInstanceState) {
                // TODO Auto-generated method stub
                super.onCreate(savedInstanceState);
                setContentView(R.layout.first_activity);
                
                //跳转到第二个界面
                Button btn = (Button) findViewById(R.id.btn);
                btn.setOnClickListener(new OnClickListener() {
                        
                        @Override
                        public void onClick(View v) {
                                // TODO Auto-generated method stub
                                Intent intent = new Intent(FirstActivity.this, SecondActivity.class).
                                      addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                                //把一个Activity转换成一个View
                                Window w = FirstGroupTab.group.getLocalActivityManager()
                                                .startActivity("SecondActivity",intent);
                            View view = w.getDecorView();
                            //把View添加大ActivityGroup中
                            FirstGroupTab.group.setContentView(view);
                        }
                });
        }
}

第一个Activity的相应的布局文件为:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    >
<TextView  
    android:layout_width="fill_parent" 
    android:layout_height="wrap_content" 
    android:text="这个是ActivityGroup中的第一个界面!"
    />
    <Button android:id="@+id/btn" 
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:text="跳转到本组中的另一个Activity中"/>
</LinearLayout>

  这就可以实现在一个TabHost内实现实现多个Activity的跳转,感兴趣的话可以试试。


  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
好的,下面我将为你提供一个简单的示例代码,演示如何在Android Studio中动态添加项目的TabHost。 1. 首先,在XML布局文件中添加TabHost组件: ```xml <TabHost android:id="@android:id/tabhost" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_below="@+id/toolbar"> <LinearLayout android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical"> <TabWidget android:id="@android:id/tabs" android:layout_width="match_parent" android:layout_height="wrap_content" /> <FrameLayout android:id="@android:id/tabcontent" android:layout_width="match_parent" android:layout_height="match_parent" /> </LinearLayout> </TabHost> ``` 2. 在Java代码中找到对应的TabHost组件并初始化: ```java TabHost tabHost = (TabHost) findViewById(android.R.id.tabhost); tabHost.setup(); ``` 3. 创建一个新的TabSpec对象,用于描述每个标签页: ```java TabHost.TabSpec spec; spec = tabHost.newTabSpec("tag1"); spec.setIndicator("Tab1"); spec.setContent(R.id.tab1); tabHost.addTab(spec); ``` 4. 重复步骤3,以添加其他标签页: ```java spec = tabHost.newTabSpec("tag2"); spec.setIndicator("Tab2"); spec.setContent(R.id.tab2); tabHost.addTab(spec); spec = tabHost.newTabSpec("tag3"); spec.setIndicator("Tab3"); spec.setContent(R.id.tab3); tabHost.addTab(spec); ``` 5. 最后,你可以在代码中动态添加标签页: ```java TabHost.TabSpec spec = tabHost.newTabSpec("tag4"); spec.setIndicator("Tab4"); spec.setContent(new TabHost.TabContentFactory() { @Override public View createTabContent(String tag) { TextView textView = new TextView(MainActivity.this); textView.setText("This is Tab 4"); return textView; } }); tabHost.addTab(spec); ``` 这个示例演示了如何在Android Studio中动态添加项目的TabHost。希望对你有所帮助!
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值