一个tabHost的setCurrentTab [英] setCurrentTab of a tabHost

查看:304
本文介绍了一个tabHost的setCurrentTab的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要问,如果我们可以称之为一个tabhost一个setCurrentTab从另一个类与包含tabhost和tabspecs类?

I'm asking if we can call a setCurrentTab on a tabhost from another class than the class that contains the tabhost and tabspecs?

我们可以把

tabHost.setCurrentTab(1);

在另一个类不止这一个:

in another class than this one :

public class Main extends TabActivity{

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.v("", "Welcome in Main");
    setContentView(R.layout.tab);

    TabHost tabHost = (TabHost)findViewById(android.R.id.tabhost);  // Le TabHost a des Tabs

    TabSpec firstTabSpec = tabHost.newTabSpec("tid1");  // TabSpec: new tab - TabSpec : setContent to the tab
    firstTabSpec.setIndicator("Informations", getResources().getDrawable(R.drawable.database)).setContent(new Intent(this,FirstTab.class));
    tabHost.addTab(firstTabSpec);

    TabSpec secondTabSpec = tabHost.newTabSpec("tid1");
    secondTabSpec.setIndicator("Graphiques", getResources().getDrawable(R.drawable.chart)).setContent(new Intent(this,SecondTab.class));
    tabHost.addTab(secondTabSpec);

    TabSpec thirdTabSpec = tabHost.newTabSpec("tid1");  // tid1 is firstTabSpec Id (used to access outside)
    thirdTabSpec.setIndicator("Réglages", getResources().getDrawable(R.drawable.settings)).setContent(new Intent(this,ThirdTab.class));
    tabHost.addTab(thirdTabSpec);
}

}

我们可以把它的静态无功?我们如何能做到这一点?

Can we make it a static var? how Can we achieve this?

感谢您的关注!

推荐答案

您当然可以。对于您的情况,TabActivity的单身可能是最好的。例如:

You sure can. For your situation, a singleton of the TabActivity may work best. For example:

public class Main extends TabActivity{
    private staic Main theInstance;

    public static getInstance() {
        return Main.theInstance;
    }

    public Main() {
        Main.theInstance = this;
    }

    // The rest of your code here.
}

然后,从另一个类,您只需拨打:

Then, from another class, you can just call:

Main.getInstance()getTabHost()setCurrentTab(1);

Main.getInstance().getTabHost().setCurrentTab(1);

注:我所提供的不是一个完整的单身实现,但它应该足够了,你在做什么。根据你的设置选项卡中的类,你可能要检查 Main.getInstance()不为null之前调用了 setCurrentTab( )方法。

NOTE: What I have provided is not a complete singleton implementation, but it should suffice for what you're doing. Depending on the class you're setting the tab from, you may want to check that Main.getInstance() is not null before calling the setCurrentTab() method.

这篇关于一个tabHost的setCurrentTab的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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