我有一个带3个标签的android程序。当我点击“地图选项卡”时,我希望它显示“地图已选定”信息,如下图所示。但如果我单击“名称”选项卡,然后第二次返回“地图选项卡”,则“再次选择地图”消息不会显示在屏幕上。如何使“地图”选项卡在每次选择该选项卡时显示“再次选择地图”消息?您能编辑下面的代码并用一个关于如何使用ontabChangeListener的示例来演示吗?
public class MapsActivity extends MapActivity implements OnTabChangeListener {
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main4);
Toast.makeText(getApplicationContext(),"Map is Selected", Toast.LENGTH_LONG).show();
}
public void onTabChanged(String tabId) {
Toast.makeText(getApplicationContext(),"Map Selected Again", Toast.LENGTH_LONG).show();
}
@Override
protected boolean isRouteDisplayed() {
return false;
} }
更新。但以下方法不输出消息。
`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;
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("Map").setIndicator("Map",res.getDrawable(R.drawable.ic_tab_map)).setContent(intent);
tabHost.addTab(spec);
tabHost.setCurrentTab(2);
}
public void onTabChanged(String tabId) {
mTabHost = getTabHost();
mTabHost.setOnTabChangedListener(this);
Toast.makeText(getApplicationContext(), "To check for Display", Toast.LENGTH_LONG).show();
Log.i("selected tab index", "Current index - "+ mTabHost.getCurrentTab());
}}
最佳答案
应该对tabactivity类而不是tab的内容实现OnTabChangeListener
。
在tabactivity中实现ontabchangelister
然后为tabhost设置侦听器mTabHost.setOnTabChangedListener(this);
@Override
public void onTabChanged(String tabId) {
Log.i("selected tab ", tabId);
}
更新
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("Map").setIndicator("Map",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());
}}