海我是新来的这个在Android的碎片开发。我的问题是片段之间的导航。我在片段1中有一个按钮,在片段2中有一个文本视图,并且我有活动activity1,我在xml中声明了这两个片段。我的问题是,当我按下fragment1中的按钮时,它必须携带一些文本并在fragment2的textview中显示,然后才必须检查fragment2是否在相同布局中?在android中的碎片导航
代码对我很有帮助。在此先感谢..........
海我是新来的这个在Android的碎片开发。我的问题是片段之间的导航。我在片段1中有一个按钮,在片段2中有一个文本视图,并且我有活动activity1,我在xml中声明了这两个片段。我的问题是,当我按下fragment1中的按钮时,它必须携带一些文本并在fragment2的textview中显示,然后才必须检查fragment2是否在相同布局中?在android中的碎片导航
代码对我很有帮助。在此先感谢..........
首先宣布你吹气成onCreateView(在2ndFragmentClass)象下面这样:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
请考虑fragment_content必须在租赁有其自身内部一个TextView(以便我们在片段内设置它的值)。然后,我们必须从第一个片段中更改此文本的值。所以,我们我们的第二个片段(包含TextView的片段)内像下面添加此构造函数:
public void setText(String name) {
TextView txt= (TextView) getView().findViewById(R.id.textView1);
txt.setNewText(name);
}
简单地说,它会像下面:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_content, container, false);
return view;
}
public void setText(String name) {
TextView txt= (TextView) getView().findViewById(R.id.textView1);
txt.setNewText(name);
}
然后我们必须定义文本必须设置成2ndFragment from 1stFragmentClass。然后我们通过按下第一个片段中的按钮来设置第二个片段的文本,如下所示:
@Override
public void onListItemClick(ListView l, View v, int position, long id) {
String url = "Hello, This is the text from 1st Fragment:)";
//Here we try to declare 2nd fragment.
2ndFragmentClass fragment = (2ndFragmentClass) getFragmentManager()
.findFragmentById(R.id.detailFragment);
fragment.setNewText(url);
}
如果您觉得它有用,请将其标记为真实答案。 –
为什么你不会只使用Google?这个主题有很多教程。 – Egor
你能否给我提供一些链接? –