2016-05-11 65 views
-1

如何在一个活动中将数据发送到另一个片段?如何在一个活动中将数据发送到另一个片段?

我有两个使用Android Studio设计视图编辑器创建的片段。我在我的MainActivity上创建了这两个片段。 fragment1是第一个片段的ID,它只包含EditText和一个按钮。 fragment2是第二个片段的ID,它只包含textView。

如何将fragment1的EditText中的数据发送到fragment2的textView?

我在下面写了一些代码,请检查它。

MainActivity.java

package com.example.radioswiba.belajar2buahfragment; 

import android.net.Uri; 
import android.support.v7.app.AppCompatActivity; 
import android.os.Bundle; 
import android.view.View; 

public class MainActivity extends AppCompatActivity implements Fragment1.OnFragmentInteractionListener, Fragment2.OnFragmentInteractionListener{ 

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

    @Override 
    public void onFragmentInteraction(Uri uri) { 

    } 
} 

Fragment1.java

//this code was generated by Android Studio 
//i have deleted some unused code and comments 

package com.example.radioswiba.belajar2buahfragment; 

import android.content.Context; 
import android.net.Uri; 
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 Fragment1 extends Fragment { 

    //let's define some of variable 
    private EditText text_input; 
    private Button button_send; 

    private OnFragmentInteractionListener mListener; 

    public Fragment1() { 
     // Required empty public constructor 
    } 

    //this generated by Android Studio 
    public static Fragment1 newInstance(String param1, String param2) { 
     Fragment1 fragment = new Fragment1(); 
     Bundle args = new Bundle(); 
     args.putString(ARG_PARAM1, param1); 
     args.putString(ARG_PARAM2, param2); 
     fragment.setArguments(args); 
     return fragment; 
    } 

    //this generated by Android Studio 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     if (getArguments() != null) { 
      mParam1 = getArguments().getString(ARG_PARAM1); 
      mParam2 = getArguments().getString(ARG_PARAM2); 
     } 
    } 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
          Bundle savedInstanceState) { 
     // Inflate the layout for this fragment 
     // my code here 
     View rootView = inflater.inflate(R.layout.fragment_fragment1, container, false); 
     text_input = (EditText) rootView.findViewById(R.id.status_text); 
     button_send = (Button) rootView.findViewById(R.id.post_btn); 
     button_send.setOnClickListener(postStatus); 
     return rootView; 
    } 

    View.OnClickListener postStatus = new View.OnClickListener(){ 
     @Override 
     public void onClick(View v){ 
      text_of_me = text_input.getText().toString(); 
      // 
      //WHAT SHOULD I WRITE HERE? 
      //SHOULD I USED BUNDLE? 


     } 
    }; 

    // TODO: Rename method, update argument and hook method into UI event 
    public void onButtonPressed(Uri uri) { 
     if (mListener != null) { 
      mListener.onFragmentInteraction(uri); 
     } 
    } 

    @Override 
    public void onAttach(Context context) { 
     super.onAttach(context); 
     if (context instanceof OnFragmentInteractionListener) { 
      mListener = (OnFragmentInteractionListener) context; 
     } else { 
      throw new RuntimeException(context.toString() 
        + " must implement OnFragmentInteractionListener"); 
     } 
    } 

    @Override 
    public void onDetach() { 
     super.onDetach(); 
     mListener = null; 
    } 

    public interface OnFragmentInteractionListener { 
     // TODO: Update argument type and name 
     void onFragmentInteraction(Uri uri); 
    } 
} 

Fragment2.java

//the code almost same with Fragment1.java 

我有在stackoverflow上搜索类似的quenstion,但我无法弄清楚。我发现有很多的解决方案如下图所示:

Fragment fragment = new Fragment(); 
Bundle bundle = new Bundle(); 
bundle.putInt(key, value); 
fragment.setArguments(bundle); 

在那里,我们创建一个新片段,同时,我也对我的活动两个碎片,我从文件中手动创建 - >新建 - 从机器人工作室菜单>新片段。我应该使用上面的代码创建新的片段吗?

回答

相关问题