Fragment间数据交互(一)
来源:互联网 发布:趋势预测 java算法 编辑:程序博客网 时间:2024/05/16 06:27
需求:显示如图效果,当点击“点我获取下边数据“按钮是Toast弹出"bottom"; 点击“点我获取上边数据”时Toast弹出top。
MainActivity中主要代码:
public class MainActivity extends FragmentActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Fragment leftFragment = new LeftFragment();
Fragment rightFragment = new RightFragment();
//用Fragment替换掉布局文件上边部分
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.top_fragment, leftFragment, "TOP").commit();
//用Fragment替换掉布局文件中下边部分
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.bottom_fragment, rightFragment, "BOTTOM").commit();
}
LeftFragment中主要代码:
public class LeftFragment extends Fragment{
private View view;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
// TODO Auto-generated method stub
Button left_button = (Button) view.findViewById(R.id.left_button);
//实现点击上边的按钮得到下边Fragmen中EditText中的数据
left_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//关键代码
Fragment mFragment = getActivity().getSupportFragmentManager().findFragmentByTag("BOTTOM");
EditText editText = (EditText) mFragment.getView().findViewById(R.id.et_bottom);
//获取到EditText中的值
String etValue = editText.getText().toString().trim();
Toast.makeText(getActivity(), etValue, 1).show();
}
});
super.onActivityCreated(savedInstanceState);
}
//加载布局文件到Fragment页面上来
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_top, null);
return view;
}
}
RightFragment中主要代码:
public class RightFragment extends Fragment{
private View view;
@Override
public void onActivityCreated(Bundle savedInstanceState) {
Button bottom_button = (Button) view.findViewById(R.id.bottom_button);
//实现点击下边Fragmen中的按钮得到上边Fragmen中EditText中的值
bottom_button.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//关键代码
Fragment mFragment = getActivity().getSupportFragmentManager().findFragmentByTag("TOP");
EditText editText = (EditText) mFragment.getView().findViewById(R.id.et_bottom);
String topValue = editText.getText().toString().trim();
Toast.makeText(getActivity(), topValue, 1).show();
}
});
super.onActivityCreated(savedInstanceState);
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_bottom, null);
return view;
}
}
布局文件代码:
activity_main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:id="@+id/top_fragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/darker_gray" />
<LinearLayout
android:id="@+id/bottom_fragment"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00FF80" />
</LinearLayout>
activity_top.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/left_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_left"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="top" />
<Button
android:id="@+id/left_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我获取下边数据"/>
</LinearLayout>
activity_bottom.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/bottom_ll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<EditText
android:id="@+id/et_bottom"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="bottom" />
<Button
android:id="@+id/bottom_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="点我获取上边数据"/>
</LinearLayout>
- Fragment间数据交互(一)
- Fragment的数据交互 一 使用Handler
- Fragment与Fragment之间的数据交互
- 探索Fragment(2)---数据交互
- 数据交互一
- JSP数据交互(一)
- 使用Fragment实现简单的数据交互
- activity与fragment之间的数据交互
- fragment与activity实现数据交互
- EventBus实现activity跟fragment交互数据
- Android Activity与Fragment 数据交互
- Fragment与Activity之间的数据交互
- Fragment与Activity之间的数据交互
- android Fragment与Activity之间数据交互
- fragment和activity之间进行数据交互
- android中fragment简单使用以及fragment之间数据交互
- Fragment信息交互之Fragment获取DialogFragment的返回数据
- Fragment信息交互之Fragment获取DialogFragment的返回数据
- Android - TabHost 选项卡功能用法布局详细讲解(2)
- velocity判断空值
- Oracle控制语句(if , loop , while , for)
- 求助scanf()如何判断输入结束
- memset
- Fragment间数据交互(一)
- JVM内存管理优化经验
- xcode刷新的问题
- 【C++系列小结】面向过程的编程风格
- java基础知识总结
- 懒惰懈怠是最大的敌人,身体是,思想上面更为严重
- MyEclipse工程配置
- SqlServer2000还原数据库报物理文件名有误解决方案
- C++继承和多态例子