将多个数据从一个片段发送到另一个片段

时间:2016-05-25 06:19:10

标签: java android

在我的app..one mainActivity ..片段A和FragmentB两个片段..    我创建了......我在FragmentA中添加了两个editText。我想将这个editText数据发送到FramentB ..并重新使用它..FragmentB..how来做这个...    我应该使用界面概念......或者是他们的任何其他概念..

    public class FragmentA extends Fragment  {
      Button nextt;
       EditText number;

      EditText alpha;
       }
         @Override
         public void onStart() {
          super.onStart();
           fm = ((MainActivity) context).getFragmentManager();
      }

      @Override
       public void onClick(View view) {
          int viewId = view.getId();
          FragmentTransaction ft;
          ft = fm.beginTransaction();
         FragmentB fragmentB = new FragmentB();
          ft.replace(R.id.frame_content, fragmentB);
          ft.addToBackStack(null);
           ft.commit();
     }

     @Override
       public View onCreateView(LayoutInflater inflater, ViewGroup container,     Bundle savedInstanceState) {
           View view = null;

            view = inflater.inflate(R.layout.fragment_a, container, false);

           nextt = (Button) view.findViewById(R.id.Button1);
             nextt.setOnClickListener(this);

            number = (EditText) view.findViewById(R.id.swTypeE);

           alpha = (EditText) view.findViewById(R.id.numE);
         }
   }
   }

2 个答案:

答案 0 :(得分:0)

//输入值

YourNewFragment ldf = new YourNewFragment ();
Bundle args = new Bundle();
args.putString("YourKey", "YourValue");
ldf.setArguments(args);

//给片段充气

getFragmentManager().beginTransaction().add(R.id.container, ldf).commit();

在新片段的onCreateView中:

//检索值

String value = getArguments().getString("YourKey");

答案 1 :(得分:0)

 @Override
 public void onClick(View view) {
 int viewId = view.getId();
 FragmentTransaction ft;
 ft = fm.beginTransaction();
 Bundle bundle = new Bundle();
 bundle.putInt("yourkey",viewId);
 FragmentB fragmentB = new FragmentB();
 fragmentB.setArguments(bundle);
 ft.replace(R.id.frame_content, fragmentB);
 ft.addToBackStack(null);
 ft.commit();}

在fragmentB中获取值

String value = getArguments().getString("yourkey");