您應該實現OnTabChangeListener
到TabActivity
類而不是Tab的內容。
在你TabActivity實施OnTabChangeListener
然後設置偵聽器TabHost mTabHost.setOnTabChangedListener(this);
實施例1,
@Override
public void onTabChanged(String tabId) {
Log.i("selected tab ", tabId);
}
例2
public class HelloTabWidget extends TabActivity implements OnTabChangeListener{
private TabHost mTabHost;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Resources res = getResources();
TabHost tabHost = getTabHost();
TabHost.TabSpec spec;
Intent intent;
mTabHost = getTabHost();
intent = new Intent().setClass(this, BarActivity.class);
spec = tabHost.newTabSpec("Name").setIndicator("Name",res.getDrawable(R.drawable.ic_tab_name)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, CityActivity.class);
spec = tabHost.newTabSpec("city").setIndicator("City",res.getDrawable(R.drawable.ic_tab_city)).setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, MapsActivity.class);
spec = tabHost.newTabSpec("Country").setIndicator("Country",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
mTabHost.setOnTabChangedListener(this);
}
public void onTabChanged(String tabId) {
Toast.makeText(getApplicationContext(), "Selected Tab "+tabId, Toast.LENGTH_LONG).show();
Log.i("selected tab index", "Current index - "+ mTabHost.getCurrentTab());
}}
你有事件一派是什麼? – azizbekian
http://stackoverflow.com/a/30904479/2784838 –
setOnTabSelectedListener已棄用。 – jay