2016-02-15 49 views
0

我想要做的事情:
FirstFragment中的用戶可以輸入他的體重。
在SecondFragment中,重量應顯示在TextView中
值應該在用戶的滑動時傳遞。
當用戶滑動時將EditText.getText從FirstFragment傳遞到SecondFragment

我試着圍繞3如何做和閱讀了很多關於片段,但我仍然找不到合適的解決方案。由於我是一個新的碎片,它也可能是我做了一個不尋常的方式來生成碎片,因此它不起作用,但我還沒弄明白。

,你可以SE unerneath居然有在代碼中的錯誤,因爲我試圖通過在MainActivity

要simplyfy以訪問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; 
    } 
} 

回答

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

這將是最簡單的方法。使用持久存儲來傳遞一個整數是矯枉過正的。 –

+0

首先感謝您的建議,我現在將其實施到我的應用程序。但有一件事我仍然得不到。當用戶滑動屏幕時,我需要將EditText.getText傳遞給SecondFragment,因爲bevore,他無法向EditText輸入任何內容。我怎樣才能做到這一點? –

+0

我的答案之間的想法是,你不會將EditText.getText傳遞給SecondFragment,但SecondFragment會檢索它。這可以在片段的onStart方法中通過調用活動中的getWeight方法來完成...... – malrok44

0

不要試圖「通過」喜歡你的價值通常會想到當活動調用一個新片段。

而應考慮使用一些持久性存儲來存儲該值,例如SharedPreferences或Sqlite數據庫。每個片段可以根據需要讀取和寫入數值。

相關問題