Get index of selected tab in tabHost

不问归期 提交于 2019-11-30 14:01:27

问题


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?


回答1:


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

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());
        }       
    });  
...
...
...



回答2:


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




回答3:


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

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

int current = tabHost.getTabHost() ;


来源:https://stackoverflow.com/questions/3583405/get-index-of-selected-tab-in-tabhost

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!