Android 获取 tabHost 中选定选项卡的索引

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3583405/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 10:51:01  来源:igfitidea点击:

Get index of selected tab in tabHost

androidandroid-2.2-froyoandroid-tabhost

提问by Hyman

I'm trying to store the index of the currently selected tab in onSaveInstanceState so I can restore it. However the getCurrentTab apparantely gives me back the String I used in the etTabHost().newTabSpec, which I find a bit weird since the documentation says it returns an int and setCurrentTab also taking an int. Does anyone know how I can get the index of my currently selected tab so I can restore it?

我正在尝试将当前选定选项卡的索引存储在 onSaveInstanceState 中,以便我可以恢复它。然而,getCurrentTab 显然返回了我在 etTabHost().newTabSpec 中使用的字符串,我觉得这有点奇怪,因为文档说它返回一个 int 并且 setCurrentTab 也接受一个 int。有谁知道如何获取当前选择的选项卡的索引以便恢复它?

回答by Jorgesys

you are on the right way, use setOnTabChangedListenerto get your selected tab.

您走对了,用于setOnTabChangedListener获取您选择的选项卡。

public class MainActivity extends TabActivity {
    static TabHost mytabs;

    mytabs = getTabHost();
    mytabs.setOnTabChangedListener(new OnTabChangeListener() {
        @Override
        public void onTabChanged(String arg0) {         
            Log.i("***Selected Tab", "Im currently in tab with index::" + mytabs.getCurrentTab());
        }       
    });  
...
...
...

回答by Pranav

You can use getCurrentTab() that returns index of tab start from 0.

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

回答by Mi.HTR

Use tabHost.getCurrentTab() to get Tab ...

使用 tabHost.getCurrentTab() 获取 Tab ...

tabHost= getTabHost();
tabHost.addTab(tab0); // TabSpec tab0=tabHost.newTabSpec(...
tabHost.addTab(tab1); //  TabSpec tab1=tabHost.newTabSpec

int current = tabHost.getTabHost() ;