閱讀244 返回首頁    go 阿裏雲 go 技術社區[雲棲]


用ActivityGroup解決TabHost中多個Activity跳轉問題

最近在做一個程序,剛開始沒考慮全,就用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="https://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="https://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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
	android:orientation="vertical" android:layout_width="fill_parent"
	android:layout_height="fill_parent">

	<TabHost xmlns:andro
		android: 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:
				android:layout_width="fill_parent" android:layout_height="wrap_content" />
			<FrameLayout android:
				android:layout_width="fill_parent" android:layout_height="wrap_content"
				android:layout_weight="3">

			</FrameLayout>


		</LinearLayout>
	</TabHost>

</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. @Override
  9. protected void onCreate(Bundle savedInstanceState) {
  10. // TODO Auto-generated method stub
  11. super.onCreate(savedInstanceState);
  12. setContentView(R.layout.main);
  13. tabHost = getTabHost();
  14. tabHost.addTab(tabHost.newTabSpec("tab1")
  15. .setIndicator("First")
  16. .setContent(new Intent(this,FirstGroupTab.class)));//第一個選項卡使用一個ActivityGroup
  17. tabHost.addTab(tabHost.newTabSpec("tab2")
  18. .setIndicator("Second")
  19. .setContent(new Intent(this, SecondTab.class)));//第二個選項卡是一個Activity
  20. tabHost.setCurrentTab(0);
  21. }
  22. }
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的管理

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. * 一個靜態的ActivityGroup變量,用於管理本Group中的Activity
  15. */
  16. public static ActivityGroup group;
  17. @Override
  18. protected void onCreate(Bundle savedInstanceState) {
  19. // TODO Auto-generated method stub
  20. super.onCreate(savedInstanceState);
  21. group = this;
  22. }
  23. @Override
  24. public void onBackPressed() {
  25. // TODO Auto-generated method stub
  26. // super.onBackPressed();
  27. //把後退事件交給子Activity處理
  28. group.getLocalActivityManager()
  29. .getCurrentActivity().onBackPressed();
  30. }
  31. @Override
  32. protected void onResume() {
  33. // TODO Auto-generated method stub
  34. super.onResume();
  35. //把界麵切換放到onResume方法中是因為,從其他選項卡切換回來時,
  36. //調用搞得是onResume方法
  37. //要跳轉的界麵
  38. Intent intent = new Intent(this, FirstActivity.class).
  39. addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  40. //把一個Activity轉換成一個View
  41. Window w = group.getLocalActivityManager().startActivity("FirstActivity",intent);
  42. View view = w.getDecorView();
  43. //把View添加大ActivityGroup中
  44. group.setContentView(view);
  45. }
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

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. Button btn = (Button) findViewById(R.id.btn);
  22. btn.setOnClickListener(new OnClickListener() {
  23. @Override
  24. public void onClick(View v) {
  25. // TODO Auto-generated method stub
  26. Intent intent = new Intent(FirstActivity.this, SecondActivity.class).
  27. addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
  28. //把一個Activity轉換成一個View
  29. Window w = FirstGroupTab.group.getLocalActivityManager()
  30. .startActivity("SecondActivity",intent);
  31. View view = w.getDecorView();
  32. //把View添加大ActivityGroup中
  33. FirstGroupTab.group.setContentView(view);
  34. }
  35. });
  36. }
  37. }
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代碼 複製代碼 收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="https://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>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:andro
    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: 
    	android:layout_width="fill_parent"
    	android:layout_height="wrap_content"
    	android:text="跳轉到本組中的另一個Activity中"/>
</LinearLayout>

最後更新:2017-04-02 22:16:28

  上一篇:go [cocos2d]如何實現模態對話框
  下一篇:go API Demos 2.3 學習筆記 (17)-- Views-&gt;Tabs