Start Android App in a specific tab

我的梦境 提交于 2019-12-22 21:21:08

问题


The question is: I have a TabHost with 4 tabs (see code below) and I got a Button in MainMenuActivity class. The Button is set up with a OnClickListener and if it is clicked I want it to go to the second tab. I have tried with setCurrentTab(1) but that just messed the project up. What can I do?

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:


tabHost.setCurrentTab(index) is the right way to go. What's the problem when you use it?

"setCurrentTab(int) opens the tab to be displayed by default, specified by the index position of the tab."



来源:https://stackoverflow.com/questions/8507160/start-android-app-in-a-specific-tab

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!