在特定选项卡中启动Android App

时间:2011-12-14 15:32:23

标签: java android android-tabhost

问题是:我有一个TabHost有4个标签(请参阅下面的代码),我在Button课程中获得MainMenuActivityButton设置为OnClickListener,如果点击,我希望它转到第二个标签。我试过setCurrentTab(1),但这搞砸了项目。我该怎么办?

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    setContentView(R.layout.main);
    setTabs() ; 
}
private void setTabs()
{
    addTab("Home", R.drawable.tab_home, MainMenuActivity.class);
    addTab("Calculate", R.drawable.tab_search, SpinnerClass.class);

    addTab("Search", R.drawable.tab_home, ScrollView1.class);
    addTab("Premium", R.drawable.tab_search, ScrollView2.class);

}

private void addTab(String labelId, int drawableId, Class<?> c)
{
    TabHost tabHost = getTabHost();
    Intent intent = new Intent(this, c);
    TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId); 

    View tabIndicator = LayoutInflater.from(this).inflate(R.layout.tab_indicator, getTabWidget(), false);
    TextView title = (TextView) tabIndicator.findViewById(R.id.title);
    title.setText(labelId);
    ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
    icon.setImageResource(drawableId);

    spec.setIndicator(tabIndicator);
    spec.setContent(intent);
    tabHost.addTab(spec);

}    

1 个答案:

答案 0 :(得分:0)

tabHost.setCurrentTab(index)是正确的方法。你使用它时会出现什么问题?

“setCurrentTab(int)打开默认显示的选项卡,由选项卡的索引位置指定。”