TabHost 中的活动

发布于 2024-12-24 01:00:38 字数 444 浏览 1 评论 0原文

我使用 TabHost。 下面的代码调用AActivity。

intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);

它位于选项卡中。 但在 AActivity 中我称之为 BActivity。 BActivity 将打开新页面,但不在选项卡中。 如何让它出现在标签框上? AActivity使用下面的代码来调用BActivity:

it = new Intent(this, BActivity.class);
startActivity(it);

I use a TabHost.
The below code to call AActivity.

intent = new Intent().setClass(this, AActivity.class);
spec = tabHost.newTabSpec("A").setIndicator("A", res.getDrawable(R.drawable.icon)).setContent(intent);
tabHost.addTab(spec);

And it is in the tab.
But in AActivity I call BActivity.
The BActivity will open new page, but not in the tab.
How to let it on the tab frame?
AActivity use below code to call BActivity:

it = new Intent(this, BActivity.class);
startActivity(it);

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(3

浅笑轻吟梦一曲 2024-12-31 01:00:38

如果您想在选项卡中打开多个活动,然后在活动地点使用活动组作为标准选项卡,并在此活动组中切换视图以在单个选项卡中打开多个活动,

您可以从 本教程

If you want to open multiple activity in Tab then on Place of activity use Activity group for par tab and switch view in this activity group for open multiple Activity in single tab

you can take some help from this tutorial

用心笑 2024-12-31 01:00:38

您需要更改当前选定的选项卡。 TabHost 类中有一个名为 setCurrentTabByTag(String tag) 的方法可以执行此操作,只需传递选项卡的标签名称(在您的情况下为 B),或者您可以使用 setCurrentTab(int index) 并传递选项卡索引。

例子。通常我有一个 MainActivity 类,这是我的 TabActivity。在此类中,我将在 onCreate 方法上创建所需的所有选项卡。

首先,我使用选项卡索引设置一些静态 int 。

    // Tab index.
    public static int FIRST_TAB = 0;
    public static int SECOND_TAB = 1;

随后,我在 onCreate 方法中创建选项卡。

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // Setting the content view.
     setContentView(R.layout.main);
     // Getting the TabHost object.
     mTabHost = getTabHost();
     // Declaring the Intent object for the tabs.
     Intent intent;
     // Creating the First tab.
     intent = new Intent().setClass(this, MyFirstActivity.class);
     addTab(mTabHost, "First", FIRST_TAB, intent)
     // Creating the Second tab.
     intent = new Intent().setClass(this, MySecondActivity.class);
     addTab(mTabHost, "Second", SECOND_TAB, intent);
     // Setting the current tab.
     switchTab(FIRST_TAB);
}

public void addTab(TabHost host, String title, String tag, Intent intent) {
     TabHost.TabSpec spec = host.newTabSpec(tag);
     spec.setContent(intent);
     spec.setIndicator(title);
     host.addTab(spec);
}

最后一个方法将更改当前选项卡。

public void switchTab(int index) {
    mTabHost.setCurrentTab(index);
}

稍后,在 MyFirstActivity 内部,您可以调用 MainActivity swichTab 方法并传递选项卡的索引来更改它。
您可以调用 Activity 类的 getParent() 方法来检索 MainActivity。

MainActivity parent = (MainActivity)getParent();

You need to change the current selected tab. There's a method called setCurrentTabByTag(String tag) in the TabHost class that will do that, just pass the tag name of your tab (which in your case is B), or you can use the setCurrentTab(int index) and pass the tab index.

Example. Usually I have a MainActivity class, which is my TabActivity. Inside of this class, I will create all tabs that I need on the onCreate method.

First I set some static int with the tabs indexes.

    // Tab index.
    public static int FIRST_TAB = 0;
    public static int SECOND_TAB = 1;

Later, I create my tabs in the onCreate method.

 @Override
 public void onCreate(Bundle savedInstanceState) {
     super.onCreate(savedInstanceState);
     // Setting the content view.
     setContentView(R.layout.main);
     // Getting the TabHost object.
     mTabHost = getTabHost();
     // Declaring the Intent object for the tabs.
     Intent intent;
     // Creating the First tab.
     intent = new Intent().setClass(this, MyFirstActivity.class);
     addTab(mTabHost, "First", FIRST_TAB, intent)
     // Creating the Second tab.
     intent = new Intent().setClass(this, MySecondActivity.class);
     addTab(mTabHost, "Second", SECOND_TAB, intent);
     // Setting the current tab.
     switchTab(FIRST_TAB);
}

public void addTab(TabHost host, String title, String tag, Intent intent) {
     TabHost.TabSpec spec = host.newTabSpec(tag);
     spec.setContent(intent);
     spec.setIndicator(title);
     host.addTab(spec);
}

And last the method that will change the current tab.

public void switchTab(int index) {
    mTabHost.setCurrentTab(index);
}

Later, inside of the MyFirstActivity you can call the MainActivity swichTab method and pass the index of the tab to change it.
You can retrieve the MainActivity calling the getParent() method of the Activity class.

MainActivity parent = (MainActivity)getParent();
花期渐远 2024-12-31 01:00:38

在创建 tabhost 的选项卡活动类中,实现以下方法。

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

在 AActivity/BActivity 中实现以下方法并在任何事件(您需要的)上调用它:

public void switchTabInActivity(long indexTabToSwitchTo){
            TabActivity tabActivity;
            tabActivity = (TabActivity) this.getParent();
            tabActivity.switchTab(indexTabToSwitchTo);
}

这里 TabActivity 是创建 tabhost 的类

In the tab activity class where the tabhost is created, implement the following method.

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

In AActivity/BActivity implement the following method and call it on any event(that you need):

public void switchTabInActivity(long indexTabToSwitchTo){
            TabActivity tabActivity;
            tabActivity = (TabActivity) this.getParent();
            tabActivity.switchTab(indexTabToSwitchTo);
}

Here TabActivity is the class where tabhost is created

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