您的位置:首页 > 其它

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

2013-05-03 17:01 531 查看
转自:/article/4012582.html

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

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







两个选项卡的实现

布局文件 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>


Java代码实现 MainActivity.java

package hkp.test;

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);
}

}


使用 ActivityGroup的管理

package hkp.test;

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

/**
* @author HuangKaipeng hkp2006@gmail.com
* 2011-10-5
*
*/
public class FirstGroupTab extends ActivityGroup {

/**
* 一个静态的ActivityGroup变量,用于管理本Group中的Activity
*/
public static ActivityGroup group;

@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

group = this;

}

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

@Override
protected void onResume() {
// TODO Auto-generated method stub
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

package hkp.test;

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;

/**
* @author HuangKaipeng hkp2006@gmail.com
* 2011-10-5
*
*/
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);
}
});
}

}

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"
>
<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>


源码下载地址:

http://download.csdn.net/detail/hutengfei0701/5327171
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐