2013-11-03 35 views
0

我有三個標籤在我的ABS TabActivity在點擊鏈接從活動到ABS specifik標籤

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

在與按鈕的活動,你可以在意向指定一個額外的:

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); 
} 
+0

它完美的工作,謝謝! – user1810991