0

我有一个带有两个选项卡的 TabHost。我想知道用户何时单击任一选项卡。我怎样才能做到这一点?

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);
4

1 回答 1

1

我认为您想使用 onTabChangeListener (http://developer.android.com/reference/android/widget/TabHost.OnTabChangeListener.html) 并将其传递给 TabHost。请参阅http://developer.android.com/reference/android/widget/TabHost.html#setOnTabChangedListener%28android.widget.TabHost.OnTabChangeListener%29

每次有人切换标签时,您都可以有效地得到通知。

于 2011-05-03T21:33:32.650 回答