如何检查在Android中打开哪个选项卡tabview

问题描述:

我想查看选择了哪个选项卡并根据该选择执行特定的代码。我已经使用以下教程来创建包含图像的列表,问题是列表项目位于单独的选项卡中,我所关注的代码仅设置第一个选项卡的图像。如何检查在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类中有两个方法,每个方法都运行相应的图标和文本。我需要在特定的选项卡上调用这些选项。

谢谢!

+1

tabhost在Android中现已弃用。所以我会建议不要使用它。 –

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

如果你仍然想使用已弃用的(你不应该),那么你需要实现OnTabChangeListener然后就可以看到这片被称为public void onTabChanged(String tabId)

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