如何在Android中更改按钮单击上的标签? [英] How to change tab on button click in Android?

查看:92
本文介绍了如何在Android中更改按钮单击上的标签?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

FragmentPagerSupport是FragmentActivity类,FragmentA和FragmentB代表2个不同的选项卡.在第一个选项卡中,我有一个EditText和一个按钮.我的任务是单击按钮时需要打开第二个选项卡,并在第二个选项卡中显示EditText值.我也在使用FragmentStatePagerAdapter.

FragmentPagerSupport is a FragmentActivity class, FragmentA and FragmentB are representing 2 different tabs. In the First tab I have a EditText and a button. My task is to on button click need to open the 2nd tab and show the EditText value in the 2nd tab. I am also using a FragmentStatePagerAdapter.

以下代码正在建立FragmentActivity的onCreate()标签:

Following code is building tabs onCreate() of FragmentActivity:

    for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
        actionBar.addTab(actionBar.newTab()
                .setText(mSectionsPagerAdapter.getPageTitle(i))
                .setTabListener(this));
    }

我在FragmentStatePagerAdapter中的代码如下:

My code in FragmentStatePagerAdapter is as follow:

public Fragment getItem(int position) {
    Fragment fragment = null;
    Bundle args = new Bundle();         
    switch (position) {
    case 0:
        fragment = FragmentA();
        args.putInt(FragmentA.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
    break;
    case 1:
        fragment = new FragmentB();
        args.putInt(FragmentB.ARG_SECTION_NUMBER, position + 1);
        fragment.setArguments(args);
    break;
    }
return fragment;
}

单击按钮时我的FragmentA代码如下:

My code of FragmentA on button click is as follow:

confirmButton.setOnClickListener(new View.OnClickListener() {
    public void onClick(View view) {


    }
}

现在我被困在这里.找不到此处的代码来调用第二个选项卡,并根据tab1 EditText值显示该值.

Now I stuck here. Can't find what should be the code here to call the 2nd tab and show the value according to tab1 EditText value.

推荐答案

假定您的ViewPager名为mPager:

Assuming your ViewPager is named mPager:

public void onClick(View view)
{
     mPager.setCurrentItem(1); // Change to page 1, i.e., FragmentB
}

我建议详细阅读培训页面.它介绍了一些用于将信息从Fragment传递到Fragment的策略(通常是通过让Activity实现一个Fragment可以调用的接口).

I would suggest reading this training page in detail. It goes into some of the strategies used to pass information from Fragment to Fragment (generally by having the Activity implement an interface that the Fragment can then call).

这实际上取决于片段的耦合程度.您当然可以使用类似的

It really depends on how coupled you want your Fragments to be. You could certainly use something like

((FragmentB)getActivity().getSupportFragmentManager().findFragmentByTag(
    "android:switcher:" + pager.getId() + ":1")).setExitText(text)

将文本从FragmentA传递到FragmentB,但这将您的Fragment彼此紧密耦合,并与Activity包含的ViewPager紧密耦合.

to pass the text from FragmentA to FragmentB, but that tightly couples your Fragments to each other and to the ViewPager the Activity contains.

我建议

  1. 使您的Activity实现一个接口,该接口包含单个方法,例如goToFragmentB(String exitText)
  2. 在FragmentA的onClick中调用此方法
  3. 具有goToFragmentB:
    • 在FragmentB中调用setExitText(按照
  1. Make your Activity implement an Interface that contains a single method such as goToFragmentB(String exitText)
  2. Call this method in your FragmentA's onClick
  3. Have goToFragmentB:
    • Call a setExitText in FragmentB (use the above findFragmentByTag using that odd tag per the ViewPager source)
    • Set the pager to FragmentB

这篇关于如何在Android中更改按钮单击上的标签?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆