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>

0 0