Android的 - 从活动中的一个选项卡中切换标签 [英] Android - Switch Tabs from within an Activity within a tab

查看:159
本文介绍了Android的 - 从活动中的一个选项卡中切换标签的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前我有每个包含一个单独的活动3个标签实施了TabHost。我的问题是我怎么选项卡之间从内位于卡主机内部的活动之一切换。我看着到处都在寻找一个真正的答案,这个问题是不成功的。

Currently I have a TabHost implemented with 3 tabs each containing a separate activity. My question is how do I switch between tabs from within one of the activities that is located inside the tab host. I've looked everywhere and have been unsuccessful in finding a real answer to this problem.

推荐答案

在这个问题我已经能够找到一个解决方案使用基于活动的标签时,切换标签争夺的很长一段时间。

After a long time of battling with this problem I've been able to find a solution to switching tabs when using activity based tabs.

在那里的tabhost创建我实现类似下面的方法父活动类:

In the parent activity class where the tabhost is created I implemented a method like the one below:

public void switchTab(int tab){
            tabHost.setCurrentTab(tab);
}

里面,我希望能够在内部切换为我创造了下面的方法的另一个选项卡的标签:

Inside of the tab that I would like to be able to switch internally to another tab I created the method below:

public void switchTabInActivity(int indexTabToSwitchTo){
            MintTrack parentActivity;
            parentActivity = (MintTrack) this.getParent();
            parentActivity.switchTab(indexTabToSwitchTo);
}

如果您想这个code一个很好的例子,你可以在我的MintTrack项目看看<一href="http://$c$c.google.com/p/minttrack/source/browse/branches/MintTrackV2/MintTrack/src/com/ponyinc/minttrack/MintTrack.java">here和<一href="http://$c$c.google.com/p/minttrack/source/browse/branches/MintTrackV2/MintTrack/src/com/ponyinc/minttrack/tabs/AuditActivity.java">here.

If you would like a good example of this code, you can take a look at my MintTrack project here and here.

作为一个侧面说明,请在决定是否使用视图或基于活动的TabHost时要非常小心。

As a side note, please be very careful when deciding whether to use view or activity based TabHost.

活动基于标签是伟大的,因为它们可以分成有自己的XML文件。活动也可以安排,而不是混乱到一成有自己的Java文件。话虽这么说的一些事情你会觉得很容易变得复杂与基于活动的标签。它很难通过标签之间的信息,而不产生额外开销。 活动基于标签也可以使用更多的内存/ CPU时间,因为他们有活动围绕他们每个人的开销。跳水之前,请考虑这一点,并在更多的权衡将使用活动根据 TabHost 。我现在知道,我会亲自与基于视图去 TabHost 如果我再次使用它们。

Activity based tabs are great because they can be separated into there own XML file. Activities can also be organized into there own Java file instead of being cluttered into one. That being said some of the things you would think would be easy become complicated with activity based tabs. Its hard to pass information between tabs without creating overhead. Activity based tabs also use more memory/CPU time as they have the overhead of the Activity around each of them. Please consider this and the many more trade offs before diving into using an Activity based TabHost. I know now that I would personally go with a view based TabHost if I ever used them again.

这篇关于Android的 - 从活动中的一个选项卡中切换标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆