简体   繁体   English

在按钮上单击,从“活动”转到“ ABS指定”选项卡

[英]On button click go from Activity to ABS specifik tab

i have three tabs in my ABS TabActivity 我的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);         
    }

In a separate Activity i have a button, on button click i want to go to the second tab . 在单独的活动中,我有一个按钮, 在按钮上单击我要转到第二个选项卡 This is what i have so far: 这是我到目前为止所拥有的:

public void onFinishGoToCreate(View view) {
        Intent myIntent = new Intent(Activity.this, TabActivity.class);
        Activity.this.startActivity(myIntent);  
    }

But this takes me to the first tab. 但这将我带到第一个选项卡。 Any ideas would be appreciated! 任何想法,将不胜感激!

In your activity with the button, you could specify an extra in the intent: 在带有按钮的活动中,您可以指定一个额外的意图:

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

Then, when you've created your tabs in the other activity, you can select the one that's specified in the incoming intent: 然后,在其他活动中创建选项卡后,可以选择传入意图中指定的选项卡:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM