【发布时间】:2011-05-03 21:17:15
【问题描述】:
我有一个带有两个选项卡的 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);
【问题讨论】:
-
您的问题不清楚。当用户单击一个选项卡时,选项卡将发生变化,并且新选项卡内的活动将变为活动状态,因此它已经“知道”其选项卡已被单击。
-
有没有办法给标签添加一个 onclick 监听器,这样我就可以在那时做一些事情?
标签: java android tabs android-tabhost tabwidget