fragment2中获取fragment1布局,找到控件设置f1里边的内容
2016-04-05 20:22
363 查看
[main]
<fragment
android:id="@+id/ll1"
android:layout_weight="1"
android:name="com.bwei.fragment_intance.F1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
</fragment>
<fragment
android:id="@+id/ll2"
android:name="com.bwei.fragment_intance.F2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
</fragment>
[f1]
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="页面一" />
[f2]
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面二" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吐司" />
【f2】/**
public class F2 extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = View.inflate(getActivity(), R.layout.f2, null);
return view;}
// 所依附的activity加载完成后会执行的方法
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO
super.onActivityCreated(savedInstanceState);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 通过id,得到静态配置的fragment
F1 f1 = (F1) getFragmentManager().findFragmentById(R.id.ll1);
// 得到f1显示的布局文件
View f1_view = f1.getView();
// 通过布局文件,由控件id得到想要的控件
TextView tv_1 = (TextView) f1_view.findViewById(R.id.tv_1);
String str = tv_1.getText().toString().trim();
Toast.makeText(getActivity(), str, 0).show();
}
});
}
}
<fragment
android:id="@+id/ll1"
android:layout_weight="1"
android:name="com.bwei.fragment_intance.F1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
</fragment>
<fragment
android:id="@+id/ll2"
android:name="com.bwei.fragment_intance.F2"
android:layout_weight="1"
android:layout_width="wrap_content"
android:layout_height="match_parent"
>
</fragment>
[f1]
<TextView
android:id="@+id/tv_1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="页面一" />
[f2]
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="页面二" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="吐司" />
【f2】/**
public class F2 extends Fragment {
private View view;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = View.inflate(getActivity(), R.layout.f2, null);
return view;}
// 所依附的activity加载完成后会执行的方法
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO
super.onActivityCreated(savedInstanceState);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() { @Override
public void onClick(View v) {
// 通过id,得到静态配置的fragment
F1 f1 = (F1) getFragmentManager().findFragmentById(R.id.ll1);
// 得到f1显示的布局文件
View f1_view = f1.getView();
// 通过布局文件,由控件id得到想要的控件
TextView tv_1 = (TextView) f1_view.findViewById(R.id.tv_1);
String str = tv_1.getText().toString().trim();
Toast.makeText(getActivity(), str, 0).show();
}
});
}
}
相关文章推荐
- android 管理应用的内存
- git命令
- 阿里云服务器Tomcat搭建以及域名绑定
- 初学android开发--day01
- 第六周项目1
- 分析驱动程序在IRQL>=DISPATCH_LEVEL时和DPC过程中不能用KeWaitForSingleObject等待对象的原因
- hdu3360+二分匹配(匈牙利算法)
- hdu-2665 Kth number(划分树)
- Dom编程的入门
- CodeForces-237C- Primes on Interval
- 第五周 项目1(3)
- NDK-JNI实战教程(四)再谈新工具及NDK开发调试
- tsung测试openfire时从CSV文件读取user信息
- tsung无密码验证
- iOS cookie值的读取和存储
- 栈的压入、弹出序列
- 高效率的素数判断方法
- Storm源码分析--Nimbus-data
- MapReduce程序运行中的排序问题
- 学习笔记(第六周)