Android - 从选项卡内的活动内切换选项卡
目前我有一个 TabHost,它实现了 3 个选项卡,每个选项卡包含一个单独的活动。我的问题是如何在位于选项卡主机内的活动之一中的选项卡之间进行切换。我到处寻找但未能找到这个问题的真正答案。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
经过长时间与这个问题的斗争,我已经找到了在使用基于活动的选项卡时切换选项卡的解决方案。
在创建 tabhost 的父活动类中,我实现了如下所示的方法:
在选项卡内部,我希望能够在内部切换到另一个选项卡,我创建了以下方法:
如果您想要一个很好的示例这段代码,你可以看看我的 MintTrack 项目 此处 和此处。
作为旁注,在决定是使用基于视图还是基于活动的 TabHost 时请非常小心。
基于
Activity
的选项卡非常棒,因为它们可以分成自己的 XML 文件。活动还可以组织到自己的 Java 文件中,而不是杂乱地组织到一个文件中。话虽这么说,一些您认为很容易的事情在基于活动的选项卡中变得复杂。在选项卡之间传递信息而不产生开销是很困难的。基于Activity
的选项卡还使用更多的内存/CPU 时间,因为它们都有围绕每个选项卡的Activity
的开销。在开始使用基于Activity
的TabHost
之前,请考虑这一点以及更多的权衡。我现在知道,如果我再次使用它们,我个人会选择基于视图的TabHost
。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.
In the parent activity class where the tabhost is created I implemented a method like the one below:
Inside of the tab that I would like to be able to switch internally to another tab I created the method below:
If you would like a good example of this code, you can take a look at my MintTrack project here and here.
As a side note, please be very careful when deciding whether to use view or activity based 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 theActivity
around each of them. Please consider this and the many more trade offs before diving into using anActivity
basedTabHost
. I know now that I would personally go with a view basedTabHost
if I ever used them again.我遇到了同样的问题。虽然所有选项卡的单一活动会更好,但有时采取简单的方法是理性的选择。
为了避免在一个选项卡想要更改为另一个选项卡时创建新的选项卡活动,我将其放入我的 AndroidManifest.xml 中:
使用您想要的选项卡发送意图:
此解决方案有缺点,但我不清楚详细信息。其他人可能知道得足以指出它们。
I encountered the same problem. While a single activity for all tabs would be better, sometimes taking the easy way out is the rational choice.
To avoid creating a new tab activity when a tab wants to change to another tab, I put this in my AndroidManifest.xml:
Send an intent with the tab you want:
This solution has drawbacks but I'm not clear over the details. Someone else might know enough to point them out.
首先,我为主类设置一个方法,该方法扩展了 TabActivity 让我们将其称为“MainActivity”
然后,我添加我的选项卡活动类;
这对我有用。
First, I set a method to my main class, which extends TabActivity let's call it "MainActivity"
Then, I add my tab activity class;
It worked for me.
步骤 #1:通过在
TabSpec
上使用更好形式的setContent()
将 tabs-holding-activities 替换为 tabs-holding-views步骤 #2:调用
在您的单个
Activity
中对TabHost
进行 setCurrentTab()我还没有看到以
Activity
作为内容有什么好处选项卡而不是简单的View
。将Activity
作为选项卡的内容会浪费 CPU 时间和内存(从而延长电池寿命),并使您尝试做的事情变得更加困难。Step #1: Replace the tabs-holding-activities with tabs-holding-views by using a better form of
setContent()
onTabSpec
Step #2: Call
setCurrentTab()
on yourTabHost
from within your singleActivity
I have yet to see any benefit to having an
Activity
be the content of a tab rather than a simpleView
. Having anActivity
as the content of the tab wastes CPU time and memory (and, hence, battery life) and makes things like you're trying to do much more difficult.我遇到了一个稍微不同的问题,我想我应该为面临类似情况的其他人添加这个问题。我有一个基于活动的选项卡式应用程序,其中一个选项卡活动启动了另一个不受 tabHost 控制的活动。我需要在此活动 finish() 上有一个按钮(即:返回主选项卡视图)并同时切换到不同的选项卡。
我决定用 BroadcastReceiver 来处理它。在设置 TabHost 的类中,我添加了此类:
..then 定义了 vars:
..then 添加到 onCreate():
最后,在新活动中,当您想要关闭该活动并切换选项卡时,请执行以下操作:
当然,您可以创建一种通过传递选项卡索引来切换到各种选项卡的方法 - 但就我而言,这种行为仅发生在一个活动中,所以我决定保持简单......
I had a slightly different problem and thought I'd add this for anyone else facing a similar situation. I have an activity-based tabbed application and one of the tab activities launches another activity which is not controlled by the tabHost. I needed to have a button on this activity finish() (ie: return back to the main Tab view) and switch to a different tab at the same time.
I decided to handle it with a BroadcastReceiver. In the class that sets up the TabHost, I added this class:
..then defined the vars:
..then added to onCreate():
Finally in the new activity when you want to close that activity and switch the tabs, do this:
Of course you could make a method to switch to various tabs by passing the tab index -- but in my case this behavior only occurs in one activity so I decided to keep it simple...
我只是放了一个
public static TabHost tabHost;
在我的
TabActivity
中。然后从任何其他选项卡我可以执行
MyTabActivity.tabHost.setCurrentTab(tabNumber);
对我来说效果很好(但我希望我从一开始就使用片段..我只是按照选项卡教程进行操作在 Android 文档中并从那里开始工作)
I just put a
public static TabHost tabHost;
in my
TabActivity
.Then from any other tab I can do a
MyTabActivity.tabHost.setCurrentTab(tabNumber);
Works fine for me (but I wish I'd used Fragments from the start.. I was just following the Tab tutorial in the Android documentation and working from there)