我正在尝试设置一个onTabChangedListner()
,我查看了 Stack Overflow 和其他一些网站,到处都是一样的东西,所以我复制了那个代码。这是我当前的代码(这都在 on 内onCreate()
)
getTabHost().setOnTabChangedListner(new onTabChangeListner() {
@Override
public void onTabChanged(String tabId) {
Toast.makeText(this, "A new tab has been selected", Toast.LENGHT_SHORT).show();
}
});
我收到 3 个错误:
未知实体
OnTabChangeListner()
没有适用的方法
(com.mycompany.myapp.MainActivity.(anonymous), java.lang.String,int)
方法
android.widget.TabHost.setOnTabChangedListner(android.widget.TabHost.OnTabChangeListner) in 'android.widget.TabHost'can not be applied to (com.mycompany.myapp.MainActivity.(anonymous))
任何帮助,将不胜感激!
更新:这是我的整个 onCreate 后跟 onTabChanged 方法:
@Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); 设置内容视图(R.layout.main);
TabHost tabHost = getTabHost();
TabSpec cat1spec = tabHost.newTabSpec("Category 1");
cat1spec.setIndicator("Category 1");
cat1spec.setContent(R.id.tab1c);
TabSpec cat2spec = tabHost.newTabSpec("Category 2");
cat2spec.setIndicator("Category 2");
cat2spec.setContent(R.id.tab2c);
TabSpec cat3spec = tabHost.newTabSpec("Favourites");
cat3spec.setIndicator("Favourites");
cat3spec.setContent(R.id.tab3c);
tabHost.addTab(cat1spec);
tabHost.addTab(cat2spec);
tabHost.addTab(cat3spec);
getActionBar().setHomeButtonEnabled(true);
tabHost.setOnTabChangedListener(this);
}
@Override
public void onTabChanged(String tabId) {
//Something will happends here
}