我的ABS TabActivity
中有三个标签public class TabActivity extends SherlockFragmentActivity {
private ViewPager mViewPager;
private TabAdapter mTabsAdapter;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getOverflowMenu();
mViewPager = new ViewPager(this);
mViewPager.setId(R.id.pager);
setContentView(mViewPager);
final ActionBar bar = getSupportActionBar();
bar.setIcon(R.drawable.actionbar_icon);
bar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
mTabsAdapter = new TabAdapter(this, mViewPager);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.tab_icon_join)), 1Fragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.tab_icon_create)), 2Fragment.class, null);
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.tab_icon_play)), 3Fragment.class, null);
}
在单独的活动中我有一个按钮,按下按钮我想转到第二个标签。 这就是我到目前为止所做的:
public void onFinishGoToCreate(View view) {
Intent myIntent = new Intent(Activity.this, TabActivity.class);
Activity.this.startActivity(myIntent);
}
但是这会把我带到第一个标签。任何想法将不胜感激!
答案 0 :(得分:0)
在使用按钮的活动中,您可以在意图中指定一个额外的内容:
public void onFinishGoToCreate(View view) {
Intent myIntent = new Intent(Activity.this, TabActivity.class);
myIntent.putExtra("selectedTab", 1); // 0 based index for selecting navigation items
Activity.this.startActivity(myIntent);
}
然后,当您在其他活动中创建了标签时,您可以选择在传入意图中指定的标签:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
...
mTabsAdapter.addTab(bar.newTab().setText("").setIcon(getResources().getDrawable(R.drawable.tab_icon_play)), 3Fragment.class, null);
int selectedTab = getIntent().getIntExtra("selectedTab", 0); // Default to first tab if nothing has been specified.
bar.setSelectedNavigationItem(selectedTab);
}