如何检查Android tabview中打开的选项卡

时间:2013-08-02 04:36:22

标签: java android tabview

我想知道选择哪个标签并根据该选择执行特定代码。我使用以下教程创建带有图像的列表,问题是列表项位于单独的选项卡中,我遵循的代码仅设置第一个选项卡的图像。

http://www.debugrelease.com/2013/06/24/android-listview-tutorial-with-images-and-text/

以下是我设置标签的方法。

        TabHost tabHost = getTabHost();

        //Tab for Students
        TabSpec studentSpec = tabHost.newTabSpec("Students");
        //set the tile and icon
        studentSpec.setIndicator("Students", getResources().getDrawable(R.drawable.icon_students));
        Intent songsIntent = new Intent(this, StudentsTab.class);
        studentSpec.setContent(songsIntent);

        //Tab for Support
        TabSpec supportSpec = tabHost.newTabSpec("Support");
        //set the tile and icon
        supportSpec.setIndicator("Support", getResources().getDrawable(R.drawable.icon_support));
        Intent videosIntent = new Intent(this, SupportTab.class);
        supportSpec.setContent(videosIntent);

        //tabHost.addTab(homeSpec);
        tabHost.addTab(studentSpec);
        tabHost.addTab(supportSpec);

    }

所以我在本教程的Model.java类中有两个方法,每个方法都运行相应的图标和文本。我需要在特定选项卡上调用这些。

谢谢!

2 个答案:

答案 0 :(得分:1)

正如armaan Stranger指出的那样,标签已过时,您应该使用操作栏标签。有一个good sample on how to create swipe tabs on android.developer

如果您仍想使用已弃用的(不应该),则需要实施OnTabChangeListener,然后您可以在public void onTabChanged(String tabId)中查看调用哪个标签。

答案 1 :(得分:0)

您可以使用getCurrentTab()返回tab的索引,从0开始。