[英]Sending data from one fragment to another fragment in two different layout
[英]sending multiple data from one fragment to another fragment
在我的應用程序中。一個mainActivity ..兩個片段,例如片段A和FragmentB ..我創建了..然后在FragmentA中添加了兩個editText ..我想將此editText數據發送到FramentB ..並在其中重用它。.FragmentB..怎么做...我應該使用接口概念..或..是他們的任何其他概念..
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);
}
}
}
//輸入值
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");
@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");
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.