Android Fragment与Fragment之间数据获取

上一篇介绍了,Fragment从Activity获取数据。这篇介绍两个Fragment之间的如何获取数据。

MainActivity包含有两个Fragment,LeftFragment和RightFragment,如果Leftfragment想要获取RightFragment中的数据,那么首先要通过FragmentManager获取的RightFragment的实例,然后获取到RightFragment的布局View,然后就可以通过View获取其EditText输入的值。右侧获取左侧的值也是相同原理。

实现效果:

点击右侧Fragment中的按钮,获取到了左侧Fragment  EditText中的数值,然后通过吐司显示出来。


源代码:


布局文件:

activity_main:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     tools:context=".MainActivity" >  
  6.   
  7.     <LinearLayout  
  8.         android:id="@+id/left"  
  9.         android:layout_width="wrap_content"  
  10.         android:layout_height="match_parent"  
  11.         android:layout_weight="1"  
  12.         android:background="#CCCCCC"  
  13.         android:orientation="vertical" >  
  14.     </LinearLayout>  
  15.   
  16.     <LinearLayout  
  17.         android:id="@+id/right"  
  18.         android:layout_width="wrap_content"  
  19.         android:layout_height="match_parent"  
  20.         android:layout_weight="1"  
  21.         android:orientation="vertical" >  
  22.     </LinearLayout>  
  23.   
  24. </LinearLayout>  

left.xml:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <EditText  
  9.         android:id="@+id/editText1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:ems="10" >  
  13.   
  14.         <requestFocus />  
  15.     </EditText>  
  16.   
  17.     <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="获取右侧的Fragment中的数据" />  
  22.   
  23. </LinearLayout>  

right.xml:

[html]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="wrap_content"  
  5.     android:orientation="vertical"  
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <EditText  
  9.         android:id="@+id/editText1"  
  10.         android:layout_width="match_parent"  
  11.         android:layout_height="wrap_content"  
  12.         android:ems="10" >  
  13.   
  14.         <requestFocus />  
  15.     </EditText>  
  16.   
  17.     <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="match_parent"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="获取左侧的Fragment中的数据" />  
  22.   
  23. </LinearLayout>  

代码:

MainActivity:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fragmentdemo7_commutetwofrag;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.FragmentManager;  
  5. import android.app.FragmentTransaction;  
  6. import android.os.Bundle;  
  7. /** 
  8.  * 该Activity包括两个Fragment:LeftFragment和RightFragment 
  9.  * 
  10.  */  
  11. public class MainActivity extends Activity {  
  12.     private FragmentManager manager;  
  13.     private FragmentTransaction transaction;  
  14.   
  15.     @Override  
  16.     protected void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.activity_main);  
  19.   
  20.         manager = getFragmentManager();  
  21.         transaction = manager.beginTransaction();  
  22.         /** 
  23.          * 将左右两个Fragment添加到MainActivity中。 
  24.          */  
  25.         LeftFragment leftFragment = new LeftFragment();  
  26.         RightFragment rightFragment = new RightFragment();  
  27.         transaction.replace(R.id.left, leftFragment, "left");  
  28.         transaction.replace(R.id.right, rightFragment, "right");  
  29.           
  30.         transaction.commit();  
  31.     }  
  32.   
  33. }  

LeftFragment:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fragmentdemo7_commutetwofrag;  
  2.   
  3. import android.app.Fragment;  
  4. import android.app.FragmentManager;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12. /** 
  13.  * 左侧的Fragment 
  14.  * 
  15.  */  
  16. public class LeftFragment extends Fragment {  
  17.     private Button button;  
  18.     private FragmentManager manager;  
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         manager = getFragmentManager();  
  23.     }  
  24.   
  25.     @Override  
  26.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  27.             Bundle savedInstanceState) {  
  28.         View view = inflater.inflate(R.layout.left, null);  
  29.         button = (Button) view.findViewById(R.id.button1);  
  30.         /** 
  31.          * 点击左侧Fragment中的按钮,获取右侧输入在EditText中的数据,以吐司形式显示出来。 
  32.          */  
  33.         button.setOnClickListener(new View.OnClickListener() {  
  34.               
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 /** 
  38.                  * 通过FragmentManager实例化对象,通过Tag获取到RightFragment,然后再获取到该View中的editText中的数据。 
  39.                  */  
  40.                 RightFragment rightFragment = (RightFragment) manager.findFragmentByTag("right");  
  41.                 String msg = ((EditText)rightFragment.getView().findViewById(R.id.editText1)).getText().toString();  
  42.                 Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();  
  43.                   
  44.             }  
  45.         });  
  46.           
  47.         return view;  
  48.     }  
  49.   
  50. }  

RightFragment:

[java]  view plain copy 在CODE上查看代码片 派生到我的代码片
  1. package com.fragmentdemo7_commutetwofrag;  
  2.   
  3. import android.app.Fragment;  
  4. import android.app.FragmentManager;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.Button;  
  10. import android.widget.EditText;  
  11. import android.widget.Toast;  
  12. /** 
  13.  * 右侧的Fragment 
  14.  */  
  15. public class RightFragment extends Fragment {  
  16.     private Button button;  
  17.     private FragmentManager manager;  
  18.     @Override  
  19.     public void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.           
  22.         manager = getFragmentManager();  
  23.     }  
  24.   
  25.     @Override  
  26.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  27.             Bundle savedInstanceState) {  
  28.         View view = inflater.inflate(R.layout.right, null);  
  29.         button = (Button) view.findViewById(R.id.button1);  
  30.         /** 
  31.          * 点击右侧Fragment中的按钮,获取左侧输入在EditText中的数据,以吐司形式显示出来。 
  32.          */  
  33.         button.setOnClickListener(new View.OnClickListener() {  
  34.               
  35.             @Override  
  36.             public void onClick(View v) {  
  37.                 /** 
  38.                  * 通过FragmentManager实例化对象,通过Tag获取到LeftFragment,然后再获取到该View中的editText中的数据。 
  39.                  */  
  40.                 LeftFragment leftFragment = (LeftFragment) manager.findFragmentByTag("left");  
  41.                 String msg = ((EditText)leftFragment.getView().findViewById(R.id.editText1)).getText().toString();  
  42.                 Toast.makeText(getActivity(), msg, Toast.LENGTH_SHORT).show();  
  43.             }  
  44.         });  
  45.         return view;  
  46.     }  
  47.   
  48. }  
  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值