当用户滑动时,将EditText.getText从FirstFragment传递给SecondFragment

时间:2016-02-15 19:17:01

标签: java android android-fragments android-viewpager swipe

我正在做的事情:
在FirstFragment中,用户可以输入他的体重 在SecondFragment中,重量应显示在TextView中 价值应该在用户轻扫时传递。

我试过arround 3 how-to's并阅读了很多关于片段但我仍然找不到合适的解决方案。因为我对片段有点新意,所以我也可以制作一种不常见的方法来生成片段,因此它不起作用,但我还是想不通。

实际上你可以在下面找到一个错误,因为我试图通过MainActivity访问FirstFragment的方法

简单地说,我没有提出两个片段xlm的整个代码。
first_frag.xml有一个EditText(id:getWeight)框,您只能输入最多3位数的数字 second_frag.xml有一个TextView(id:txtAlcLvl)

activity_main.xml中:

<android.support.v4.view.ViewPager
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/viewPager"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
/>

MainActivity.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;


public class MainActivity extends FragmentActivity {

    Calculator calc = new Calculator();
    ViewPager pager;
    private MyPagerAdapter myPagerAdapter;
    String TabFragmentB;



    public void setTabFragmentB(String t){
        TabFragmentB = t;
    }

    public String getTabFragmentB(){
        return TabFragmentB;
    }

    public Calculator getCalc(){
        return this.calc;
    }

    public void getWeight(){
        FragmentManager fm = getSupportFragmentManager();
        FirstFragment fragment = (FirstFragment)fm.findFragmentById(R.id.i_dont_know_the_id);
        //failure because trying to get access to Method of FirstFragment
        fragment.getWeight();
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.activity_main);


        final MyPagerAdapter pagerAdapter = new MyPagerAdapter(getSupportFragmentManager());
        pager = (ViewPager) findViewById(R.id.viewPager);
        pager.setAdapter(pagerAdapter);


        pager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(final int i, final float v, final int i2) {
            }

            @Override
            public void onPageSelected(final int i) {
                YourFragmentInterface fragment = (YourFragmentInterface) pagerAdapter.instantiateItem(pager, i);
                if (fragment != null) {
                    fragment.fragmentBecameVisible();
                }
            }



            @Override
            public void onPageScrollStateChanged(final int i) {
            }
        });

    }


    public interface YourFragmentInterface {
        void fragmentBecameVisible();
    }

    private class MyPagerAdapter extends FragmentStatePagerAdapter {

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

        @Override
        public Fragment getItem(int position) {
            switch(position) {

                case 1: return new SecondFragment(); //SecondFragment.newInstance("SecondFragment, Instance 1");

                case 0: return new FirstFragment();

                default: return new FirstFragment(); //FirstFragment.newInstance("FirstFragment, Default");
            }
        }




        @Override
        public int getCount() {
            return 2;
        }
    }
}

FirstFragment.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;

public class FirstFragment extends Fragment implements MainActivity.YourFragmentInterface {

    Button btnBeer;
    Button btnBeerSmall;
    Button btnWine;
    Button btnLiq;
    Button btnSch;
    Button btnWater;
    Button btnMale;
    Button btnFemale;
    Calculator calc;
    EditText getWeight;

    public String getWeight(){
        return getWeight.getText().toString();
    }

    @Override
    public void fragmentBecameVisible() {



        // You can do your animation here because we are visible! (make sure onViewCreated has been called too and the Layout has been laid. Source for another question but you get the idea.
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.first_frag, container, false);
        calc = ((MainActivity) getActivity()).getCalc();

        btnBeer = (Button) v.findViewById(R.id.btnBeer);
        btnBeerSmall = (Button) v.findViewById(R.id.btnBeerSmall);
        btnWine = (Button) v.findViewById(R.id.btnWine);
        btnLiq = (Button) v.findViewById(R.id.btnLiq);
        btnSch = (Button) v.findViewById(R.id.btnSch);
        btnWater = (Button) v.findViewById((R.id.btnWater));
        btnMale = (Button) v.findViewById(R.id.btnMale);
        btnFemale = (Button) v.findViewById(R.id.btnFemale);
        getWeight = (EditText) v.findViewById(R.id.getWeight);

        btnBeer.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calc.addConsumption(0);
            }
        });

        btnBeerSmall.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calc.addConsumption(1);
            }
        });

        btnWine.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calc.addConsumption(2);
            }
        });

        btnLiq.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calc.addConsumption(3);
            }
        });

        btnSch.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calc.addConsumption(4);
            }
        });

        btnWater.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calc.addConsumption(5);
            }
        });

        btnMale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnMale.setBackgroundResource(R.drawable.male_false);
                btnFemale.setBackgroundResource(R.drawable.female);
            }
        });

        btnFemale.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                btnFemale.setBackgroundResource(R.drawable.female_false);
                btnMale.setBackgroundResource(R.drawable.male);
            }
        });


        return v;

    }

    public static FirstFragment newInstance(String text) {

        FirstFragment f = new FirstFragment();
        Bundle b = new Bundle();
        b.putString("msg", text);


        f.setArguments(b);

        return f;
    }
}

SecondFragment.java:

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageButton;
import android.widget.TextView;



public class SecondFragment extends Fragment implements MainActivity.YourFragmentInterface {


    TextView txtAlcLvl;
    TextView txtTimeToZero;
    TextView txtPeak;
    ImageButton btnReset;
    Calculator calc;
    String weight;



    @Override
    public void fragmentBecameVisible() {
        calc.person.setSex(false);
        calc.person.setWeight(80);
        txtAlcLvl.setText(((MainActivity) getActivity()).getWeight());
        //txtAlcLvl.setText(String.format("%.2f", calc.getCurrentLevel()) + "‰");

        // You can do your animation here because we are visible! (make sure onViewCreated has been called too and the Layout has been laid. Source for another question but you get the idea.
    }

    public void setSex(){

    }

    public void setWeight(){
        try{

        }   catch(NumberFormatException e){

        }
    }


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View v = inflater.inflate(R.layout.second_frag, container, false);
        calc = ((MainActivity) getActivity()).getCalc();
        String myTag = getTag();
        ((MainActivity)getActivity()).setTabFragmentB(myTag);

        txtAlcLvl = (TextView) v.findViewById(R.id.txtAlcLvl);
        txtTimeToZero = (TextView) v.findViewById(R.id.txtTimeToZero);
        txtPeak = (TextView) v.findViewById(R.id.txtPeak);
        btnReset = (ImageButton) v.findViewById((R.id.btnReset));

        btnReset.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                calc.resetConsumption();
            }
        });

        return v;
    }

    public void updateText(String t){
        txtAlcLvl.setText(t);
    }

    public static SecondFragment newInstance(String text) {

        SecondFragment f = new SecondFragment();
        Bundle b = new Bundle();
        b.putString("msg", text);

        f.setArguments(b);

        return f;
    }
}

2 个答案:

答案 0 :(得分:0)

不要试图&#34;通过&#34;与活动调用新片段时通常所期望的值。

相反,请考虑使用一些持久存储来存储值,例如SharedPreferences或Sqlite数据库。每个片段都可以根据需要从中读取和写入值。

答案 1 :(得分:0)

你可以在中途完成。 不要试图从片段中获取活动的权重,而是尝试从片段中设置活动的权重。

public class MainActivity extends FragmentActivity {
  ...
  private float weight;

  public void setWeight(float weight) {
    this.weight = weight;
  }

  public float getWeight() {
    return this.weight();
  }

然后在你的FirstFragment中:

((MainActivity)getActivity()).setWeight(...);

最后在你的SecondFragment中:

float weight = ((MainActivity)getActivity()).getWeight();