上一篇介绍了,Fragment从Activity获取数据。这篇介绍两个Fragment之间的如何获取数据。
MainActivity包含有两个Fragment,LeftFragment和RightFragment,如果Leftfragment想要获取RightFragment中的数据,那么首先要通过FragmentManager获取的RightFragment的实例,然后获取到RightFragment的布局View,然后就可以通过View获取其EditText输入的值。右侧获取左侧的值也是相同原理。
实现效果:
点击右侧Fragment中的按钮,获取到了左侧Fragment EditText中的数值,然后通过吐司显示出来。
源代码:
布局文件:
activity_main:
- <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"
- tools:context=".MainActivity" >
- <LinearLayout
- android:id="@+id/left"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:background="#CCCCCC"
- android:orientation="vertical" >
- </LinearLayout>
- <LinearLayout
- android:id="@+id/right"
- android:layout_width="wrap_content"
- android:layout_height="match_parent"
- android:layout_weight="1"
- android:orientation="vertical" >
- </LinearLayout>
- </LinearLayout>
left.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="wrap_content"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <EditText
- android:id="@+id/editText1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10" >
- <requestFocus />
- </EditText>
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="获取右侧的Fragment中的数据" />
- </LinearLayout>
right.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="wrap_content"
- android:orientation="vertical"
- tools:context=".MainActivity" >
- <EditText
- android:id="@+id/editText1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:ems="10" >
- <requestFocus />
- </EditText>
- <Button
- android:id="@+id/button1"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="获取左侧的Fragment中的数据" />
- </LinearLayout>
代码:
MainActivity:
- package com.fragmentdemo7_commutetwofrag;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- /**
- * 该Activity包括两个Fragment:LeftFragment和RightFragment
- *
- */
- public class MainActivity extends Activity {
- private FragmentManager manager;
- private FragmentTransaction transaction;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- manager = getFragmentManager();
- transaction = manager.beginTransaction();
- /**
- * 将左右两个Fragment添加到MainActivity中。
- */
- LeftFragment leftFragment = new LeftFragment();
- RightFragment rightFragment = new RightFragment();
- transaction.replace(R.id.left, leftFragment, "left");
- transaction.replace(R.id.right, rightFragment, "right");
- transaction.commit();
- }
- }
LeftFragment:
- package com.fragmentdemo7_commutetwofrag;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * 左侧的Fragment
- *
- */
- public class LeftFragment extends Fragment {
- private Button button;
- private FragmentManager manager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- manager = getFragmentManager();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.left, null);
- button = (Button) view.findViewById(R.id.button1);
- /**
- * 点击左侧Fragment中的按钮,获取右侧输入在EditText中的数据,以吐司形式显示出来。
- */
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- /**
- * 通过FragmentManager实例化对象,通过Tag获取到RightFragment,然后再获取到该View中的editText中的数据。
- */
- RightFragment rightFragment = (RightFragment) manager.findFragmentByTag("right");
- String msg = ((EditText)rightFragment.getView().findViewById(R.id.editText1)).getText().toString();
- Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
- }
- });
- return view;
- }
- }
RightFragment:
- package com.fragmentdemo7_commutetwofrag;
- import android.app.Fragment;
- import android.app.FragmentManager;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- /**
- * 右侧的Fragment
- */
- public class RightFragment extends Fragment {
- private Button button;
- private FragmentManager manager;
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- manager = getFragmentManager();
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container,
- Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.right, null);
- button = (Button) view.findViewById(R.id.button1);
- /**
- * 点击右侧Fragment中的按钮,获取左侧输入在EditText中的数据,以吐司形式显示出来。
- */
- button.setOnClickListener(new View.OnClickListener() {
- @Override
- public void onClick(View v) {
- /**
- * 通过FragmentManager实例化对象,通过Tag获取到LeftFragment,然后再获取到该View中的editText中的数据。
- */
- LeftFragment leftFragment = (LeftFragment) manager.findFragmentByTag("left");
- String msg = ((EditText)leftFragment.getView().findViewById(R.id.editText1)).getText().toString();
- Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();
- }
- });
- return view;
- }
- }