我认为你需要创建一个带有标签的 BaseActivity
abstract public class BaseActivity extends FragmentActivity {
}
和所有其他活动扩展 BaseActivity
编辑
给你的小例子
BaseActivity.java
abstract public class BaseActivity extends Activity implements TabListener {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
ActionBar actionBar = getActionBar();
// add tabs to actionbar
actionBar.addTab(actionBar.newTab().setText("TAB 1")
.setTabListener(this));
actionBar.addTab(actionBar.newTab().setText("TAB 2")
.setTabListener(this));
}
}
第一活动.java
public class FirstActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(getApplicationContext(),
tab.getText() + " selected in FirstActivity",
Toast.LENGTH_SHORT).show();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}
SecondActivity.java
public class SecondActivity extends BaseActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);
}
@Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
@Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
Toast.makeText(getApplicationContext(),
tab.getText() + " selected in SectondActivity",
Toast.LENGTH_SHORT).show();
}
@Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
// TODO Auto-generated method stub
}
}