简体   繁体   English

滑动选项卡:如何将变量传递到多个选项卡活动

[英]Sliding Tabs: How to pass variables to multiple tab activities

I have a MainActivity that compute 3 number results (double A, B, C) and 3 number results (double D, E, F) when a "compute" button is press. 我有一个MainActivity,当按下“计算”按钮时,可以计算3个数字结果(双A,B,C)和3个数字结果(双D,E,F)。 I then create a Sliding tab activity with 2 tabs to display the results. 然后,我创建带有2个选项卡的“滑动”选项卡活动以显示结果。 What I am trying to do is pass the variable (A, B, C) to be print in TextView of Tab-1; 我想做的是传递变量(A,B,C)以在Tab-1的TextView中打印; and pass the variables (D, E, F) to be print in TextView of Tab-2. 并传递变量(D,E,F)以在Tab-2的TextView中打印。

Where can I place the code, shown below to the MainActivity, to pass the variables to Tab-1 and Tab-2? 我在哪里可以将下面显示的代码放在MainActivity上,以将变量传递给Tab-1和Tab-2? Can anyone help or maybe I have used the wrong variable passing method? 谁能帮忙,或者我使用了错误的变量传递方法? Thank you for your help. 谢谢您的帮助。

Intent intent1 = new Intent(this, form_tab1.class );
intent1.putExtra(EX_A, A);
intent1.putExtra(EX_B, B);
intent1.putExtra(EX_C, C);

Intent intent2 = new Intent(this, form_tab2.class );
intent2.putExtra(EX_D, D);
intent2.putExtra(EX_E, E);
intent2.putExtra(EX_F, F);

Here's the code of the MainActivity java: 这是MainActivity java的代码:

package com.abc.www.apps;
import ...
public class MainActivity extends AppCompatActivity {

    public static final String EX_A = "com.abc.www.apps.EX_A";
    public static final String EX_F = "com.abc.www.apps.EX_B";
    public static final String EX_C = "com.abc.www.apps.EX_C";
    public static final String EX_D = "com.abc.www.apps.EX_D";
    public static final String EX_E = "com.abc.www.apps.EX_E";
    public static final String EX_F = "com.abc.www.apps.EX_F";

    double ax, bx, cx, dx;
    double A, B, C;
    double D, E, F;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ax = 2;
        bx = 3;
        cx = 4;
        dx = 5
    }

    //Compute button is pressed
    public void compute(View view)
    {
        A = ax+bx;
        B = ax+cx;
        C = ax+dx;
        D = bx+cx;
        E = bx+dx;
        F = cx+dx;

        //Open the Sliding Tab Activity
        Intent intent = new Intent(this, SlideTabActivity.class );
        startActivity(intent);
    }
}

Here's the code of sliding tab activity: 这是滑动标签活动的代码:

package com.abc.www.apps;
import ...
public class SlideTabActivity extends AppCompatActivity {

   /**
    * The {@link android.support.v4.view.PagerAdapter} that will provide
    * fragments for each of the sections. We use a
    * {@link FragmentPagerAdapter} derivative, which will keep every
    * loaded fragment in memory. If this becomes too memory intensive, it
    * may be best to switch to a
    * {@link android.support.v4.app.FragmentStatePagerAdapter}.
    */
   private SectionsPagerAdapter mSectionsPagerAdapter;
   public TextView outTV;

   /**
    * The {@link ViewPager} that will host the section contents.
    */
   private ViewPager mViewPager;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.slidetab_form);

      Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
      setSupportActionBar(toolbar);

      // Create the adapter that will return a fragment for each of the three
      // primary sections of the activity.
      mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

      // Set up the ViewPager with the sections adapter.
      mViewPager = (ViewPager) findViewById(R.id.container);
      mViewPager.setAdapter(mSectionsPagerAdapter);

      TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);

      mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
      tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
   }

   /**
    * A placeholder fragment containing a simple view.
    */
   public static class PlaceholderFragment extends Fragment {
      /**
       * The fragment argument representing the section number for this
       * fragment.
       */
      private static final String ARG_SECTION_NUMBER = "section_number";

      public PlaceholderFragment() {
      }

      /**
       * Returns a new instance of this fragment for the given section
       * number.
       */
      public static PlaceholderFragment newInstance(int sectionNumber) {
         PlaceholderFragment fragment = new PlaceholderFragment();
         Bundle args = new Bundle();
         args.putInt(ARG_SECTION_NUMBER, sectionNumber);
         fragment.setArguments(args);
         return fragment;
     }

     @Override
     public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
         View rootView = null;
         switch (getArguments().getInt(ARG_SECTION_NUMBER))
         {
            case 1:
               // do something
               rootView = inflater.inflate(R.layout.form_tab1, container, false);
               break;
            case 2:
               // load another page
               rootView = inflater.inflate(R.layout.form_tab2, container, false);
               break;
         }
         return rootView;
      }
   }

   /**
    * A {@link FragmentPagerAdapter} that returns a fragment corresponding to
    * one of the sections/tabs/pages.
    */
   public class SectionsPagerAdapter extends FragmentPagerAdapter {

      public SectionsPagerAdapter(FragmentManager fm) {
         super(fm);
      }

      @Override
      public Fragment getItem(int position) {
      // getItem is called to instantiate the fragment for the given page.
      // Return a PlaceholderFragment (defined as a static inner class below).
       return PlaceholderFragment.newInstance(position + 1);
      }

      @Override
      public int getCount() {
         // Show 2 total pages.
         return 2;
      }
   }
}
    send all variables to slidetabactivity and get all values there

    public void compute(View view)
        {
            A = ax+bx;
            B = ax+cx;
            C = ax+dx;
            D = bx+cx;
            E = bx+dx;
            F = cx+dx;

    Intent intent1 = new Intent(this, SlideTabActivity.class );
    intent1.putExtra(EX_A, A);
    intent1.putExtra(EX_B, B);
    intent1.putExtra(EX_C, C);
    intent1.putExtra(EX_D, D);
    intent1.putExtra(EX_E, E);
    intent1.putExtra(EX_F, F);

            //Open the Sliding Tab Activity
            Intent intent = new Intent(this, SlideTabActivity.class );
            startActivity(intent);
        }
    }

    then create a two bundle variables in slidetabActivity 
Bundle tab1=new Bundle();
tab1.putdouble(EX_A, A);
tab1.putdouble(EX_B, B);
tab1.putdouble(EX_C, C);
Bundle tab2=new Bundle();
tab2.putdouble(EX_D, D);
tab2.putdouble(EX_E, E);
tab2.putdouble(EX_F, F);

    and pass different bundle to different tab  according to your condition`enter code here`

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

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