Android - 从选项卡内的活动内切换选项卡

发布于 2024-08-26 20:56:34 字数 102 浏览 5 评论 0原文

目前我有一个 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(7

千年*琉璃梦 2024-09-02 20:56:34

经过长时间与这个问题的斗争,我已经找到了在使用基于活动的选项卡时切换选项卡的解决方案。

在创建 tabhost 的父活动类中,我实现了如下所示的方法:

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

在选项卡内部,我希望能够在内部切换到另一个选项卡,我创建了以下方法:

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

如果您想要一个很好的示例这段代码,你可以看看我的 MintTrack 项目 此处此处

作为旁注,在决定是使用基于视图还是基于活动的 TabHost 时请非常小心。

基于 Activity 的选项卡非常棒,因为它们可以分成自己的 XML 文件。活动还可以组织到自己的 Java 文件中,而不是杂乱地组织到一个文件中。话虽这么说,一些您认为很容易的事情在基于活动的选项卡中变得复杂。在选项卡之间传递信息而不产生开销是很困难的。基于 Activity 的选项卡还使用更多的内存/CPU 时间,因为它们都有围绕每个选项卡的 Activity 的开销。在开始使用基于 ActivityTabHost 之前,请考虑这一点以及更多的权衡。我现在知道,如果我再次使用它们,我个人会选择基于视图的 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:

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

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

等待圉鍢 2024-09-02 20:56:34

我遇到了同样的问题。虽然所有选项卡的单一活动会更好,但有时采取简单的方法是理性的选择。

为了避免在一个选项卡想要更改为另一个选项卡时创建新的选项卡活动,我将其放入我的 AndroidManifest.xml 中:

<activity android:name=".MyTabsActivity"
        android:label="Tabs!"
        android:launchMode="singleTask">

使用您想要的选项卡发送意图:

class OneTabContentActivity {
  void switchTab() {
    final Intent intent = new Intent(mContext, MyTabsActivity.class);
    intent.setAction("Switch to tab 1, please");
    mContext.startActivity(intent);
}

class MyTabsActivity {
  @Override
  protected void onNewIntent (Intent intent) {
    super.onNewIntent(intent);
    getTabHost().setCurrentTab(1);
  }
}

此解决方案有缺点,但我不清楚详细信息。其他人可能知道得足以指出它们。

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:

<activity android:name=".MyTabsActivity"
        android:label="Tabs!"
        android:launchMode="singleTask">

Send an intent with the tab you want:

class OneTabContentActivity {
  void switchTab() {
    final Intent intent = new Intent(mContext, MyTabsActivity.class);
    intent.setAction("Switch to tab 1, please");
    mContext.startActivity(intent);
}

class MyTabsActivity {
  @Override
  protected void onNewIntent (Intent intent) {
    super.onNewIntent(intent);
    getTabHost().setCurrentTab(1);
  }
}

This solution has drawbacks but I'm not clear over the details. Someone else might know enough to point them out.

写给空气的情书 2024-09-02 20:56:34

首先,我为主类设置一个方法,该方法扩展了 TabActivity 让我们将其称为“MainActivity”

public TabHost getMyTabHost() { return tabHost; }

然后,我添加我的选项卡活动类;

MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);

这对我有用。

First, I set a method to my main class, which extends TabActivity let's call it "MainActivity"

public TabHost getMyTabHost() { return tabHost; }

Then, I add my tab activity class;

MainActivity ta = (MainActivity) this.getParent();
TabHost th = ta.getMyTabHost();
th.setCurrentTab(0);

It worked for me.

爱她像谁 2024-09-02 20:56:34

步骤 #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() on TabSpec

Step #2: Call setCurrentTab() on your TabHost from within your single Activity

I have yet to see any benefit to having an Activity be the content of a tab rather than a simple View. Having an Activity 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.

戈亓 2024-09-02 20:56:34

我遇到了一个稍微不同的问题,我想我应该为面临类似情况的其他人添加这个问题。我有一个基于活动的选项卡式应用程序,其中一个选项卡活动启动了另一个不受 tabHost 控制的活动。我需要在此活动 finish() 上有一个按钮(即:返回主选项卡视图)并同时切换到不同的选项卡。

我决定用 BroadcastReceiver 来处理它。在设置 TabHost 的类中,我添加了此类:

class ChangeTabReceiver extends BroadcastReceiver { 
   @Override 
   public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "ChangeTabReceiver: received");
     TabHost tabHost = getTabHost();
     tabHost.setCurrentTab(1);
   } 
}

..then 定义了 vars:

ChangeTabReceiver changeTabReceiver;
IntentFilter changeTabFilter;

..then 添加到 onCreate():

changeTabReceiver = new ChangeTabReceiver();
changeTabFilter = new IntentFilter(myApplication.CHANGE_TAB); 
registerReceiver(changeTabReceiver, changeTabFilter);

最后,在新活动中,当您想要关闭该活动并切换选项卡时,请执行以下操作:

Intent intent = new Intent(myApplication.CHANGE_TAB);
this.sendBroadcast(intent); 
this.finish();

当然,您可以创建一种通过传递选项卡索引来切换到各种选项卡的方法 - 但就我而言,这种行为仅发生在一个活动中,所以我决定保持简单......

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:

class ChangeTabReceiver extends BroadcastReceiver { 
   @Override 
   public void onReceive(Context context, Intent intent) { 
     Log.d(TAG, "ChangeTabReceiver: received");
     TabHost tabHost = getTabHost();
     tabHost.setCurrentTab(1);
   } 
}

..then defined the vars:

ChangeTabReceiver changeTabReceiver;
IntentFilter changeTabFilter;

..then added to onCreate():

changeTabReceiver = new ChangeTabReceiver();
changeTabFilter = new IntentFilter(myApplication.CHANGE_TAB); 
registerReceiver(changeTabReceiver, changeTabFilter);

Finally in the new activity when you want to close that activity and switch the tabs, do this:

Intent intent = new Intent(myApplication.CHANGE_TAB);
this.sendBroadcast(intent); 
this.finish();

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

蓝眼睛不忧郁 2024-09-02 20:56:34
public void switchTab(int index){
   MintTrack ParentActivity;
   ParentActivity = (MintTrack) this.getParent();
   ParentActivity.getTabHost().setCurrentTab(index);
}
public void switchTab(int index){
   MintTrack ParentActivity;
   ParentActivity = (MintTrack) this.getParent();
   ParentActivity.getTabHost().setCurrentTab(index);
}
波浪屿的海角声 2024-09-02 20:56:34

我只是放了一个 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)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文