gpt4 book ai didi

java - fragment 到 fragment EditText

转载 作者:行者123 更新时间:2023-11-30 00:25:14 24 4
gpt4 key购买 nike

我想在单击按钮时将我的 EditText 输入从我的第一个 fragment 传递到第二个 fragment 上的 TextView 。但是当按钮点击被触发时,textview 显示“null”值。

第一个 fragment :

 @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;
}

第二个 fragment :

 @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;
}

主要 Activity :

public static Bundle myBundle = new Bundle();

最佳答案

带参数调用newInstance怎么样?

SecondFragment.newInstance(/*your text*/)  //when you open your fragment

//in your Second Fragment
public static SecondFragment newInstance(String data) {
SecondFragment sFragment = new SecondFragment ();
Bundle bundle = new Bundle();
bundle.putString("data", data);
sFragment.setArguments(bundle);
return sFragment;
}

关于java - fragment 到 fragment EditText,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45507522/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com