2016-06-26 54 views
0

javaの& Two.java) 切り替えるときeditextがありますのEditText値をセーブ&リロードフラグメント間の切り替え?は、各断片に断片

私はTUTOSを読んだ後、いくつかのことを試してみたが、何もうまく機能しません。(

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tv_one" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="one" /> 

    <EditText 
     android:layout_width="237dp" 
     android:layout_height="wrap_content" 
     android:inputType="date" 
     android:ems="10" 
     android:id="@+id/editText_One" 
     android:text="blabla" 
     android:layout_gravity="center_horizontal" /> 

</LinearLayout> 

Two.xml One.xml:

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" > 

    <TextView 
     android:id="@+id/tv_one" 
     android:layout_width="fill_parent" 
     android:layout_height="wrap_content" 
     android:text="TWO" /> 

    <EditText 
     android:layout_width="237dp" 
     android:layout_height="wrap_content" 
     android:inputT`enter code here`ype="date" 
     android:ems="10" 
     android:id="@+id/editText_Two" 
     android:layout_gravity="center_horizontal" /> 

ワン.java:

誰かが私を助けてくださいことができ

多くthnaks

答えて

0

まずあなたが断片を含む活動に、あなたは、単に2つの文字列を定義することができます文字列1、文字列2の活動への断片を添付して割り当てる必要がありますあなたはどちらかの活動またはフラグメントを切り替えることがある場合、それぞれのEditTextの値は、あなたは、件のデータの少量を保存し、それらを取得するために、共有設定を使用することができ、1 すなわち

String one=editTextOne.getText.toString(); 
String two=editTextTwo.getText.toString(); 
+0

はどこかであなたにexempleをお持ちですか? – morbak

+0

http://stackoverflow.com/questions/17580593/android-two-fragments-in-same-activity – Radwa

+0

1つは、2つのグローバル変数にする必要がありますアクティビティの手段は、任意のメソッドの定義 つまり、 = ""、String two = ""; one = editTextOne.getText.toString(); を編集します。 two = editTextTwo.getText.toString(); 共有設定も使用できます。 – Radwa

0

切り抜いします。

共有プリファレンスは、小さなセットのキーと値のペアです。

書き込み

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 

SharedPreferences.Editor editor = sharedPref.(); 

       editor.putInt(getString(R.string.saved_high_score), newHighScore); 

editor.commit(); 

読む

SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE); 

int defaultValue = getResources().getInteger(R.string.saved_high_score_default); 

long highScore = sharedPref.getInt(getString(R.string.saved_high_score), defaultValue); 

あなたは読み書きなどプットを使用して文字列として任意のプリミティブデータ型を[タイプ]得ることができます[タイプ]メソッド。

上記のソースコードは、アンドロイドの公式開発者トレーニングに由来し、Androidに属しています。

"saveInstanceStateの方法はありますか?"

このバンドルは、1つのアクティビティが破棄され、スクリーンローテーションによって作成された場合、またはコール後に再開されアクティビティとフラグメントの切り替えが行われなかった場合に便利です。

でも、試してみることができます。 idを持つEditTextのようなビューには組み込みのsaveInstanceStateがあるので、手動で処理する必要はありません。

フラグメントを切り替えるには、アクティビティクラスのスコープ付きプロパティを使用します。フラグメントとインタフェースを追加して、アクティビティと通信できるようにする必要があります。

Overview

次の例では、フラグメント切り替えるとのEditTextの値を保持する方法を示しています。

MainActivityには、FrameLayoutと2つのボタンがあります。フラグメントトランザクションは、ボタンクリックイベントで実行されます。

フラグメントには、アクティビティと通信するためのインターフェイスが含まれています。 EditText値が変更されるたびに、アクティビティのプロパティが更新されます。

アクティビティは、トランザクションの直後にEditText値を設定するためにフラグメントと通信します。すべての

まず、ここではMainActivity、FragmentAのXMLレイアウトファイルとFragmentBクラスです:

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    xmlns:tools="http://schemas.android.com/tools" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" 
    android:orientation="vertical" 
    android:weightSum="100" 
    android:paddingBottom="@dimen/activity_vertical_margin" 
    android:paddingLeft="@dimen/activity_horizontal_margin" 
    android:paddingRight="@dimen/activity_horizontal_margin" 
    android:paddingTop="@dimen/activity_vertical_margin" 
    tools:context="morbak.stackoverflow.fragmentcommunication.MainActivity"> 

    <FrameLayout 
     xmlns:android="http://schemas.android.com/apk/res/android" 
     android:id="@+id/fragment_container" 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="50"/> 

    <LinearLayout 
     android:layout_width="match_parent" 
     android:layout_height="0dp" 
     android:layout_weight="50" 
     android:orientation="horizontal" 
     android:weightSum="100"> 

     <Button 
      android:id="@+id/button1" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:text="Fragment A" /> 

     <Button 
      android:id="@+id/button2" 
      android:layout_width="0dp" 
      android:layout_height="wrap_content" 
      android:layout_weight="50" 
      android:text="Fragment B" /> 

    </LinearLayout> 

</LinearLayout> 

activity_main.xml

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/etOne" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Fragment A content"/> 

</LinearLayout> 

fragment_a.xmlは

<?xml version="1.0" encoding="utf-8"?> 

<LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android" 
    android:orientation="vertical" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent"> 

    <EditText 
     android:id="@+id/etTwo" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:hint="Fragment B content"/> 

</LinearLayout> 

fragment_b.xml

次に、フラグメントAおよびBのクラスがあります。ソースコードは自明です。

package morbak.stackoverflow.fragmentcommunication; 

import android.app.Activity; 
import android.content.Context; 
import android.os.Bundle; 
import android.support.v4.app.Fragment; 
import android.text.Editable; 
import android.text.TextWatcher; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.EditText; 

public class FragmentA extends Fragment { 

    //Views 
    EditText etOne; 

    //Fields 
    String value; 

    //Listeners 
    OnFragmentASelectedListener mCallback; 

    //Context 
    Context context; 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 

     //Inflate views and layouts 
     View rootView = inflater.inflate(R.layout.fragment_a, container, false); 

     etOne = (EditText) rootView.findViewById(R.id.etOne); 

     //Events 
     etOne.addTextChangedListener(new TextWatcher() { 
      @Override 
      public void beforeTextChanged(CharSequence s, int start, int count, int after) { 

      } 

      @Override 
      public void onTextChanged(CharSequence s, int start, int before, int count) { 

       //Execute the interface's abstract method 
       mCallback.onFragmentASelected(s.toString()); 

      } 

      @Override 
      public void afterTextChanged(Editable s) { 

      } 
     }); 

     etOne.setText(value); 

     return rootView; 
    } 

    //Set the fragment's field value 
    public void setEditText(String newValue) { 

     value = newValue; 

    } 

    //Interface 
    public interface OnFragmentASelectedListener { 
     public void onFragmentASelected(String value); 
    } 

    //Throws an exception if the activity implements FragmentA.OnFragmentASelectedListener, but not the abstract method 
    @Override 
    public void onAttach(Activity activity) { 
     super.onAttach(activity); 

     context = activity; 

     // This makes sure that the container activity has implemented 
     // the callback interface. If not, it throws an exception 
     try { 
      mCallback = (OnFragmentASelectedListener) activity; 
     } catch (ClassCastException e) { 
      throw new ClassCastException(activity.toString() 
        + " must implement OnFragmentASelectedListener"); 
     } 
    } 
} 

FragmentA.java

同じ作業をFragmentBで行われていますが、明らかに検索しFragmentBからetOne etTwoFragmentAを交換する必要がする必要があります。

は最後に、ここでフラグメントのトランザクションを処理し、断片のリスナーを使用していますMainActivityがある:

package morbak.stackoverflow.fragmentcommunication; 

import android.content.Context; 
import android.support.v4.app.FragmentTransaction; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Button; 

public class MainActivity extends AppCompatActivity implements FragmentA.OnFragmentASelectedListener, FragmentB.OnFragmentBSelectedListener { 

    Button button1; 
    Button button2; 

    FragmentTransaction transaction; 
    FragmentA fragmentA; 
    FragmentB fragmentB; 

    String editTextOne; 
    String editTextTwo; 

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

     button1 = (Button) findViewById(R.id.button1); 
     button2 = (Button) findViewById(R.id.button2); 

     editTextOne = ""; 
     editTextTwo = ""; 

     button1.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       transaction = getSupportFragmentManager().beginTransaction(); 

       fragmentA = new FragmentA(); 

       transaction.replace(R.id.fragment_container, fragmentA); 
       transaction.addToBackStack(null); 
       transaction.commit(); 

       fragmentA.setEditText(editTextOne); 

      } 
     }); 

     button2.setOnClickListener(new View.OnClickListener() { 
      @Override 
      public void onClick(View v) { 

       transaction = getSupportFragmentManager().beginTransaction(); 

       fragmentB = new FragmentB(); 

       transaction.replace(R.id.fragment_container, fragmentB); 
       transaction.addToBackStack(null); 
       transaction.commit(); 

       fragmentB.setEditText(editTextTwo); 

      } 
     }); 
    } 

    public void onFragmentASelected(String value) { 
     editTextOne = value; 
    } 

    public void onFragmentBSelected(String value) { 
     editTextTwo = value; 
    } 
} 

MainActivity.java

+0

それはsharedpreferencesで動作していますが、それは実際には作成されていません。 _Bundle savedInstanceState_の解決策はありませんか? – morbak

+0

答えを編集します。私はすぐに例を追加します。 –

+0

私はちょうどあなたの例を与えるために答えを編集を追加... –