问题
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