Android开发之Fragment与Activity的数据交互通过回调机制实现

来源:互联网 发布:库卡机器人编程手册pdf 编辑:程序博客网 时间:2024/04/29 19:04

 上一篇文章简单介绍了Android的回调机制的使用,这一篇博文将重点介绍Fragment碎片与activity的数据交互,fragment在Android开发中起着至关重要的作用,通过官方Android api我们可以看到,fragment有着自己的生命周期并依赖于它绑定的activity的生命周期而存在,那样activity与fragment怎样进行数据交互呢,请看此篇博文。

MainActiivity的代码

[java] view plain copy
  1. package com.example.f08_fragment03;  
  2.   
  3. import com.example.f08_fragment03.Fragment02.Callback;  
  4.   
  5. import android.os.Bundle;  
  6. import android.app.Activity;  
  7. import android.app.FragmentManager;  
  8. import android.app.FragmentTransaction;  
  9. import android.view.Menu;  
  10. import android.view.View;  
  11. import android.widget.Button;  
  12. import android.widget.EditText;  
  13.   
  14. public class MainActivity extends Activity {  
  15.     private FragmentManager manager;// 创建fragment管理  
  16.     private FragmentTransaction transaction;// 创建fragment的事物  
  17.     private EditText editText;  
  18.     private Button button;  
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.         editText = (EditText) this.findViewById(R.id.editText2);  
  25.         button = (Button) this.findViewById(R.id.button1);  
  26.         manager = getFragmentManager();  
  27.         transaction = manager.beginTransaction();  
  28.         final Fragment02 fragment02 = new Fragment02();  
  29.         transaction.add(R.id.left, fragment02, "left");  
  30.         transaction.commit();  
  31.         // 不管要实现怎样的功能都必须调用以下三个方法  
  32.         // manager=getFragmentManager();  
  33.         //transaction=manager.beginTransaction();  
  34.         // transaction.commit();提交数据  
  35.         button.setOnClickListener(new View.OnClickListener() {  
  36.   
  37.             @Override  
  38.             public void onClick(View arg0) {  
  39.                 // TODO Auto-generated method stub  
  40.                 //通过callback机制得到fragment的数据  
  41.                 fragment02.getString(new Callback() {  
  42.   
  43.                     @Override  
  44.                     public void getString(String msg) {  
  45.                         // TODO Auto-generated method stub  
  46.                         editText.setText(msg);  
  47.                     }  
  48.                 });  
  49.             }  
  50.         });  
  51.     }  
  52.   
  53.     @Override  
  54.     public boolean onCreateOptionsMenu(Menu menu) {  
  55.         // Inflate the menu; this adds items to the action bar if it is present.  
  56.         getMenuInflater().inflate(R.menu.main, menu);  
  57.         return true;  
  58.     }  
  59.   
  60. }  
Fragment02的代码

[java] view plain copy
  1. package com.example.f08_fragment03;  
  2.   
  3. import android.app.Activity;  
  4. import android.app.Fragment;  
  5. import android.os.Bundle;  
  6. import android.util.Log;  
  7. import android.view.LayoutInflater;  
  8. import android.view.View;  
  9. import android.view.ViewGroup;  
  10. import android.widget.Button;  
  11. import android.widget.EditText;  
  12.   
  13. public class Fragment02 extends Fragment {  
  14.     private Button button;  
  15.     private EditText editText;  
  16.   
  17.     @Override  
  18.     public void onAttach(Activity activity) {  
  19.         // TODO Auto-generated method stub  
  20.         super.onAttach(activity);  
  21.     }  
  22.   
  23.     @Override  
  24.     public void onCreate(Bundle savedInstanceState) {  
  25.         // TODO Auto-generated method stub  
  26.         super.onCreate(savedInstanceState);  
  27.     }  
  28.   
  29.     // 在该方法中更新UI  
  30.     @Override  
  31.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  32.             Bundle savedInstanceState) {  
  33.         // TODO Auto-generated method stub  
  34.   
  35.         View view = inflater.inflate(R.layout.left, null);  
  36.         button = (Button) view.findViewById(R.id.button1);  
  37.         editText = (EditText) view.findViewById(R.id.editText1);  
  38.         button.setOnClickListener(new View.OnClickListener() {  
  39.   
  40.             @Override  
  41.             public void onClick(View arg0) {  
  42.                 //通过getActivity方法得到activity的控件,以此可以得到数据  
  43.                 EditText eText = (EditText) getActivity().findViewById(  
  44.                         R.id.editText2);  
  45.                 Log.i("info""------->" + eText.getText().toString());  
  46.                 editText.setText(eText.getText().toString());  
  47.   
  48.             }  
  49.         });  
  50.         return view;  
  51.     }  
  52.   
  53.     @Override  
  54.     public void onStart() {  
  55.         // TODO Auto-generated method stub  
  56.         super.onStart();  
  57.     }  
  58.   
  59.     @Override  
  60.     public void onResume() {  
  61.         // TODO Auto-generated method stub  
  62.         super.onResume();  
  63.     }  
  64.   
  65.     @Override  
  66.     public void onPause() {  
  67.         // TODO Auto-generated method stub  
  68.         super.onPause();  
  69.     }  
  70.     //实现数据传递  
  71.     public void getString(Callback callback) {  
  72.         String msg = editText.getText().toString();  
  73.         callback.getString(msg);  
  74.     }  
  75.     //创建接口  
  76.     public interface Callback {  
  77.         public void getString(String msg);  
  78.     }  
  79.   
  80. }  

main的xml

[java] view plain copy
  1. <RelativeLayout 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.   
  6.     tools:context=".MainActivity" >  
  7.   
  8.     <LinearLayout  
  9.         android:id="@+id/left"  
  10.         android:layout_width="200dp"  
  11.         android:layout_height="match_parent"  
  12.         android:background="#cbcbcb"  
  13.         android:orientation="vertical" >  
  14.     </LinearLayout>  
  15.   
  16.     <LinearLayout  
  17.         android:id="@+id/right"  
  18.          android:layout_width="200dp"  
  19.         android:layout_height="match_parent"  
  20.         android:orientation="vertical"   
  21.         >  
  22.           
  23.           
  24.     </LinearLayout>  
  25.   
  26.     <EditText  
  27.         android:id="@+id/editText2"  
  28.         android:layout_width="wrap_content"  
  29.         android:layout_height="wrap_content"  
  30.         android:layout_alignParentTop="true"  
  31.         android:layout_marginTop="14dp"  
  32.         android:layout_toRightOf="@+id/left"  
  33.         android:ems="10"  
  34.         android:inputType="textPassword" >  
  35.   
  36.         <requestFocus />  
  37.     </EditText>  
  38.   
  39.     <Button  
  40.         android:id="@+id/button1"  
  41.         android:layout_width="wrap_content"  
  42.         android:layout_height="wrap_content"  
  43.         android:layout_below="@+id/editText2"  
  44.         android:layout_toRightOf="@+id/left"  
  45.         android:text="得到Fragment的数据" />  
  46.   
  47. </RelativeLayout>  

fragment的xml

[java] view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <EditText  
  8.         android:id="@+id/editText1"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:inputType="text"  
  12.         android:ems="10" >  
  13.   
  14.         <requestFocus />  
  15.     </EditText>  
  16.   
  17.     <Button  
  18.         android:id="@+id/button1"  
  19.         android:layout_width="wrap_content"  
  20.         android:layout_height="wrap_content"  
  21.         android:text="得到Activity的数据" />  
  22.   
  23. </LinearLayout>  

      这样就大功告成了,点击fragment的button就可以在其editText控件显示activity的editText控件的文字,点击activity的button就可以得到fragment的编辑控件的数据!
0 0
原创粉丝点击