我想將我的EditText輸入從我的第一個片段傳遞到按鈕單擊的第二個片段上的textview。但是,當按鈕點擊被觸發時,textview顯示「null」值。片段到片段EditText
第一塊碎片:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.first_frag, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragFirst);
tv.setText(getArguments().getString("msg"));
Button btn = (Button) v.findViewById(R.id.tryBtn);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
EditText woo = (EditText)v.findViewById(R.id.editText);
MainActivity.myBundle.putString("Try", String.valueOf(woo));
switch (v.getId()) {
case R.id.tryBtn:
SecondFragment home = new SecondFragment();
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.first_frag, home);
ft.setTransition(FragmentTransaction.TRANSIT_ENTER_MASK);
ft.addToBackStack(null);
ft.commit();
break;
}
}
}
);
return v;
}
第二塊碎片:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.second_frag, container, false);
TextView tv = (TextView) v.findViewById(R.id.tvFragSecond);
TextView tvTry = (TextView) v.findViewById(R.id.tvTry);
String myValue = (String) MainActivity.myBundle.get("Try");
return v;
}
MainActivity:
public static Bundle myBundle = new Bundle();
非常感謝。這工作完美..如果我要添加多個值按鈕點擊? – randolfrojo11
只是把它放在已創建的包中: bundle.putString(「secondTry」,woo2.getText()。toString());然後從參數中得到它:String myValue2 = arguments.getString(「secondTry」); –