有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

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

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

http://www.debugrelease.com/2013/06/24/安卓-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);

    }

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

谢谢大家!


共 (2) 个答案

  1. # 1 楼答案

    正如一个陌生人指出的,标签已经过时了,你应该使用actionbar标签。有一个good sample on how to create swipe tabs on android.developer

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

  2. # 2 楼答案

    可以使用getCurrentTab()返回从0开始的选项卡索引