在一个特定的标签启动Android应用 [英] Start Android App in a specific tab
问题描述
现在的问题是:我有一个 TabHost
4个选项卡(见code以下),我得到了一个按钮
在 MainMenuActivity
类。在按钮
设置了一个 OnClickListener
,如果点击它,我希望它去的第二个选项卡。我曾尝试与 setCurrentTab(1)
但只是搞砸项目了。我该怎么办?
公共无效的onCreate(捆绑savedInstanceState){
super.onCreate(savedInstanceState); 的setContentView(R.layout.main);
setTabs();
}
私人无效setTabs()
{
addTab(家,R.drawable.tab_home,MainMenuActivity.class);
addTab(计算,R.drawable.tab_search,SpinnerClass.class); addTab(搜索,R.drawable.tab_home,ScrollView1.class);
addTab(premium,R.drawable.tab_search,ScrollView2.class);}私人无效addTab(字符串labelId,INT drawableId,类<> C)
{
TabHost tabHost = getTabHost();
意向意图=新意图(这一点,C);
TabHost.TabSpec规格= tabHost.newTabSpec(标签+ labelId); 查看tabIndicator = LayoutInflater.from(本).inflate(R.layout.tab_indicator,getTabWidget(),FALSE);
TextView的标题=(TextView中)tabIndicator.findViewById(R.id.title);
title.setText(labelId);
ImageView的图标=(ImageView的)tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId); spec.setIndicator(tabIndicator);
spec.setContent(意向);
tabHost.addTab(规范);}
tabHost.setCurrentTab(指数)
是正确的道路要走。有什么问题,当你使用它吗?
的setCurrentTab(INT)打开默认显示的标签,通过标签的索引位置指定。的
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);
}
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."
这篇关于在一个特定的标签启动Android应用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!