如何更改标签在Android的? [英] How to change tabs in Android?

查看:176
本文介绍了如何更改标签在Android的?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有三个选项卡,每个人都有自己的活动。标签如下:

I have three tabs that each has its own Activity. The tabs are as follows:

Home [HomeActivity]
Search [SearchActivity]
Account [AccountActivity]

我有一个处理的主要TabHost对象的主要活动,这是它的内容:

I have a Main Activity which handles the main TabHost object and this is its content:

public TabHost tabHost;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
tabHost = getTabHost();      

tabHost.addTab(tabHost.newTabSpec("tab1").setIndicator("Home").setContent(new Intent(this, HomeActivity.class)));

tabHost.addTab(tabHost.newTabSpec("tab2").setIndicator("Search").setContent(new Intent(this, SearchActivity.class).putExtra("callX", true)));
tabHost.addTab(tabHost.newTabSpec("tab3").setIndicator("Account").setContent(new Intent(this, AccountActivity.class)));
tabHost.setCurrentTab(0);
}

现在我在搜索选项卡中的一个按钮,我需要它被点击时,无论什么时候,在主页选项卡应激活。我想我应该以某种方式呼吁 tabHost 对象 setCurrentTab()方法,但我不知道里面怎么访问在 SearchActivity 类?结果
我也许应该使用意图为这我不知道怎么用。

Now I have a button in Search tab which I need when it is clicked, no matter what, the Home tab should activate. I guess I should somehow call the setCurrentTab() method on tabHost object but I don't how to access it inside the SearchActivity class?
I probably should use Intent for that which I have no idea how to use.

推荐答案

设置的方法我的主要类,它扩展TabActivity姑且称之为MainActivity

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

public TabHost getMyTabHost() { 
return tabHost; 
}

然后加入我的标签活动课;

Then add my tab activity class;

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

或跟随一个更好的形式给出<一个href=\"http://stackoverflow.com/questions/2541802/android-switch-tabs-from-within-an-activity-within-a-tab/3031178#3031178\">at这

or follow a better aproach at this

这篇关于如何更改标签在Android的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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