2011-12-14 21 views
2

问题是:我有一个TabHost带有4个选项卡(请参阅下面的代码),我在MainMenuActivity类中获得了ButtonButton设置为OnClickListener,如果它被点击,我希望它进入第二个选项卡。我尝试过setCurrentTab(1),但这只是搞砸了项目。我能做什么?在特定标签中启动Android应用程序

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

}  

回答

0

tabHost.setCurrentTab(index)是正确的路要走。你使用它时有什么问题?

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

+0

嘿,我有一个问题。我有水平滚动视图,其中我有我的tabhost.tabHost.setCurrentTab(索引)设置当前标签,但滚动视图必须设置为滚动到当前标签不工作。请你帮我? – Nevaeh 2015-04-20 09:09:34

相关问题