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

Java 如何检查Android tabview中打开的选项卡,java,android,tabview,Java,Android,Tabview,我想看看选择了哪个选项卡,并根据该选择执行特定代码。我使用了下面的教程来创建带有图像的列表,问题是列表项位于单独的选项卡中,我遵循的代码仅为第一个选项卡设置图像 下面是我如何设置选项卡的 TabHost tabHost = getTabHost(); //Tab for Students TabSpec studentSpec = tabHost.newTabSpec("Students"); //set the tile an

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

下面是我如何设置选项卡的

        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类中有两个方法,每个方法都运行相应的图标和文本。我需要在特定的选项卡上调用这些


谢谢大家!

正如一位陌生人指出的那样,选项卡已经过时,您应该使用actionbar选项卡。有一个问题


如果您仍然想使用不推荐的(不应该)选项卡,那么您需要实现
OnTabChangeListener
,然后您可以查看在
public void onTabChanged(String tabId)

中调用哪个选项卡。您可以使用getCurrentTab()返回从0开始的选项卡索引。

现在android中不推荐使用tabhost。所以我建议不要用这个。