我已在应用程序中使用tabHost创建了4个选项卡,它工作正常。 当我点击我的第一个标签时,它会加载相应的xml页面并显示一些细节。在那个页面中,有一个按钮在那里,当我点击那个按钮时,它会使用一个Intent.My问题来加载另一个活动。如何在同一Tab中显示该活动?如何在选项卡活动中创建子活动
2
A
回答
4
您必须使用这个类
import java.util.ArrayList;
import android.app.Activity;
import android.app.ActivityGroup;
import android.app.LocalActivityManager;
import android.content.Intent;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.Window;
public class TabActivityGroup extends ActivityGroup {
private ArrayList<String> mIdList;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (mIdList == null)
mIdList = new ArrayList<String>();
}
/**
* This is called when a child activity of this one calls its finish method.
* This implementation calls {@link LocalActivityManager#destroyActivity} on
* the child activity and starts the previous activity. If the last child
* activity just called finish(),this activity (the parent), calls finish to
* finish the entire group.
*/
@Override
public void finishFromChild(Activity child) {
LocalActivityManager manager = getLocalActivityManager();
int index = mIdList.size() - 1;
if (index < 1) {
finish();
return;
}
manager.destroyActivity(mIdList.get(index), true);
mIdList.remove(index);
index--;
String lastId = mIdList.get(index);
Intent lastIntent = manager.getActivity(lastId).getIntent();
Window newWindow = manager.startActivity(lastId, lastIntent);
setContentView(newWindow.getDecorView());
}
/**
* Starts an Activity as a child Activity to this.
*
* @param Id
* Unique identifier of the activity to be started.
* @param intent
* The Intent describing the activity to be started.
* @throws android.content.ActivityNotFoundException.
*/
public void startChildActivity(String Id, Intent intent) {
Window window = getLocalActivityManager().startActivity(Id,
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
if (window != null) {
mIdList.add(Id);
setContentView(window.getDecorView());
}
}
/**
* The primary purpose is to prevent systems before
* android.os.Build.VERSION_CODES.ECLAIR from calling their default
* KeyEvent.KEYCODE_BACK during onKeyDown.
*/
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
// preventing default implementation previous to
// android.os.Build.VERSION_CODES.ECLAIR
return true;
}
return super.onKeyDown(keyCode, event);
}
/**
* Overrides the default implementation for KeyEvent.KEYCODE_BACK so that
* all systems call onBackPressed().
*/
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
onBackPressed();
return true;
}
return super.onKeyUp(keyCode, event);
}
/**
* If a Child Activity handles KeyEvent.KEYCODE_BACK. Simply override and
* add this method.
*/
@Override
public void onBackPressed() {
int length = mIdList.size();
if (length > 1) {
Activity current = getLocalActivityManager().getActivity(
mIdList.get(length - 1));
current.finish();
}
}
}
扩展此类到另一个活动,如下
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
public class InterMediateActivity extends TabActivityGroup{
String TabID;
String TabName;
@Override
public void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
TabID=getIntent().getStringExtra("TabID");
TabName=getIntent().getStringExtra("TabName");
Log.i("Tab from intermediate",""+TabID+" "+TabName);
filterTabs(TabID);
}
private void filterTabs(String TabID)
{
startChildActivity("Options", new Intent(this,HomePage.class));
}
}
而不是直接调用从标签内的活动,首先导航到InterMediateActivity
如下
Intent intent = new Intent(this, InterMediateActivity.class);
intent.putExtra("TabID", item.elementAt(0));
intent.putExtra("TabName", item.elementAt(1));
specTab.setContent(intent);
mTabHost.addTab(specTab);
相关问题
- 1. 如何在Android中创建自定义选项卡活动?
- 2. 如何从另一个活动创建动态选项卡
- 3. 如何激活tabpanel中动态创建的选项卡?
- 4. 在Android中创建首选项活动
- 5. 如何为动态创建的每个选项卡创建活动?
- 6. 如何为选项卡式活动的每个选项卡创建UI?
- 7. 如何从程序上回到子活动中的选项卡活动
- 8. 离子选项卡活动颜色
- 9. 如何禁用Android中选项卡内活动的选项卡
- 10. Android选项卡活动
- 11. 如何在angularjs中使用ui bootstrap选项卡的动态活动选项卡?
- 12. 如何创建活动活动?
- 13. 在活动中创建活动?
- 14. 如何在单击特定选项卡时清除子活动
- 15. 如何在选项卡式活动的每个选项卡中添加不同的活动性
- 16. 使用CSS指向非活动选项卡的活动选项卡?
- 17. Android Studio创建新活动没有“空白活动”选项
- 18. 从选项卡活动中打开另一个活动
- 19. API 21滑动选项卡活动
- 20. 选项卡视图活动滚动
- 21. 使用选项卡式活动刷卡
- 22. 使用选项卡外部导航栏创建选项卡式活动
- 23. 动态更改活动选项卡索引jQuery UI选项卡
- 24. 选项卡式活动中的页脚
- 25. Android Studio中的选项卡式活动
- 26. 如何将选项卡式活动添加到导航抽屉活动中?
- 27. 如何加载其他活动的选项卡中的地图活动
- 28. 切换TabActivity中的选项卡后,从头开始创建子活动
- 29. 选择活动选项卡按日期
- 30. 如何将图像放置在android的选项卡活动选项卡中?
check [this](http://united-coders.com/nico-heid/use-android-activitygroup-within-tabhost-to-show-different-activity/)tutorial and also [this](http:/ /gamma-point.com/content/android-how-have-multiple-activities-under-single-tab-tabactivity) – 2013-03-14 11:19:59