需求:显示如图效果,当点击“点我获取下边数据“按钮是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:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="@android:color/darker_gray" />
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#00FF80" />
activity_top.xml:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="top" />
activity_bottom.xml:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="bottom" />