在特定选项卡中启动 Android 应用程序

发布于 2024-12-21 13:36:43 字数 1338 浏览 2 评论 0原文

问题是:我有一个带有 4 个选项卡的 TabHost(请参阅下面的代码),并且在 MainMenuActivity 类中有一个 ButtonButton 使用 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);

}    

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

}    

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

旧瑾黎汐 2024-12-28 13:36:43

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

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

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."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文