开发者

How to determine if a user clicked on a tab inside of a TabHost?

I have a TabHost with two tabs. I want to know when the user clicks on either tab. How can I achieve this?

Resources res = getResources(); // Resource object to get Drawables
        TabHost tabHost = getTabHost();  // The activity TabHost
        TabHost.TabSpec spec;  // Resusable TabSpec for each tab
        Intent intent;  // Reusable Intent for each tab

        // Create an Intent to launch an Activity for the tab (to be reused)
        intent = new Intent().setClass(this, Main.class);

        // Initialize a TabSpec for each tab and add it to the TabHost
        spec = tabHost.newTabSpec("watchlist").setIndicator("Watchlist",res.getDrawable(R.drawable.icon)).setContent(intent);
        tabHost.addTab(spec);

        // Do the same for the other tabs
        intent = new Intent().setClass(this, ARActivity.class);
        spec = tabHost.newTabSpec("trending").setIndicator("Trending",res.getDrawable(R.drawable.icon))开发者_运维知识库.setContent(intent);
        tabHost.addTab(spec);

        tabHost.setCurrentTab(0);


I think you want to use an onTabChangeListener (http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html) and pass that to the TabHost. See http://developer.android.com/reference/android/widget/TabHost.html#setOnTabChangedListener%28android.widget.TabHost.OnTabChangeListener%29

You can effectively get notified every time someone switches a tab.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜