本文介绍了将活动 B 的字符串发送到活动 A 的片段 F的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个活动(A 和 B),我的片段 F 包含在活动 A 中.我想将字符串从活动 B 发送到片段 F.我该怎么做?这是我的代码:
I have two activities (A & B) and my fragment F is contained in the activity A. I'd like to send strings from activity B to fragment F. How can I do that? Here's my code:
活动 B
public final static String EXTRA_MESSAGE = "com.sample.MESSAGE";
// onClick get button from activity B layout
public void get(View view) {
Intent intent = new Intent(this, ActivityA.class);
TextView textView = (TextView)findViewById(R.id.coordinates);
String message = textView.getText().toString();
intent.putExtra(EXTRA_MESSAGE, message);
startActivity(intent);
}
片段 F
EditText editText;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
String num = getActivity().getIntent().getStringExtra("EXTRA_MESSAGE");
View v = inflater.inflate(R.layout.tab1, container, false);
// EditText from fragment F layout
editText = (EditText) v.findViewById(R.id.edit_coordinates);
editText.setText(num);
return v;
}
当我在我的设备上运行它时我没有收到任何错误,但我没有将字符串发送到我的 EditText
I don't get any errors when i run it on my device but I'm not getting the strings to my EditText
推荐答案
你应该在片段中用 "com.sample.MESSAGE"
替换 "EXTRA_MESSAGE"
You should replace "EXTRA_MESSAGE"
with "com.sample.MESSAGE"
in fragment
所以替换
String num = getActivity().getIntent().getStringExtra("EXTRA_MESSAGE");// OR Double quotes
与
String num = getActivity().getIntent().getStringExtra("com.sample.MESSAGE");
这篇关于将活动 B 的字符串发送到活动 A 的片段 F的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!