如何确定用户是否单击TabHost内的选项卡?

时间:2011-05-03 21:17:15

标签: java android tabs android-tabhost tabwidget

我有一个带有两个标签的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);

1 个答案:

答案 0 :(得分: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

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

相关问题