【问题标题】:How to check which tab is open in Android tabview如何检查在 Android tabview 中打开了哪个选项卡
【发布时间】:2013-08-02 04:36:21
【问题描述】:

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

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

谢谢!

【问题讨论】:

  • tabhost 现在在 android 中已被弃用。所以我建议不要使用它。

标签: java android tabview


【解决方案1】:

正如 armaan Stranger 所指出的,选项卡已经过时,您应该使用操作栏选项卡。有一个good sample on how to create swipe tabs on android.developer

如果您仍想使用已弃用的(不应该),那么您需要实现OnTabChangeListener,然后您可以看到在public void onTabChanged(String tabId) 中调用了哪个选项卡。

【讨论】:

    【解决方案2】:

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

    【讨论】:

      猜你喜欢
      • 2021-07-04
      • 1970-01-01
      • 1970-01-01
      • 2021-03-01
      • 2016-02-01
      • 2014-11-10
      • 2012-12-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多