上一篇文章简单介绍了Android的回调机制的使用,这一篇博文将重点介绍Fragment碎片与activity的数据交互,fragment在Android开发中起着至关重要的作用,通过官方Android api我们可以看到,fragment有着自己的生命周期并依赖于它绑定的activity的生命周期而存在,那样activity与fragment怎样进行数据交互呢,请看此篇博文。
MainActiivity的代码
package com.example.f08_fragment03; import com.example.f08_fragment03.Fragment02.Callback; import android.os.Bundle; import android.app.Activity; import android.app.FragmentManager; import android.app.FragmentTransaction; import android.view.Menu; import android.view.View; import android.widget.Button; import android.widget.EditText; public class MainActivity extends Activity { private FragmentManager manager;// 创建fragment管理 private FragmentTransaction transaction;// 创建fragment的事物 private EditText editText; private Button button; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); editText = (EditText) this.findViewById(R.id.editText2); button = (Button) this.findViewById(R.id.button1); manager = getFragmentManager(); transaction = manager.beginTransaction(); final Fragment02 fragment02 = new Fragment02(); transaction.add(R.id.left, fragment02, "left"); transaction.commit(); // 不管要实现怎样的功能都必须调用以下三个方法 // manager=getFragmentManager(); //transaction=manager.beginTransaction(); // transaction.commit();提交数据 button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { // TODO Auto-generated method stub //通过callback机制得到fragment的数据 fragment02.getString(new Callback() { @Override public void getString(String msg) { // TODO Auto-generated method stub editText.setText(msg); } }); } }); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } }Fragment02的代码
package com.example.f08_fragment03; import android.app.Activity; import android.app.Fragment; import android.os.Bundle; import android.util.Log; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.widget.Button; import android.widget.EditText; public class Fragment02 extends Fragment { private Button button; private EditText editText; @Override public void onAttach(Activity activity) { // TODO Auto-generated method stub super.onAttach(activity); } @Override public void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); } // 在该方法中更新UI @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { // TODO Auto-generated method stub View view = inflater.inflate(R.layout.left, null); button = (Button) view.findViewById(R.id.button1); editText = (EditText) view.findViewById(R.id.editText1); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View arg0) { //通过getActivity方法得到activity的控件,以此可以得到数据 EditText eText = (EditText) getActivity().findViewById( R.id.editText2); Log.i("info", "------->" + eText.getText().toString()); editText.setText(eText.getText().toString()); } }); return view; } @Override public void onStart() { // TODO Auto-generated method stub super.onStart(); } @Override public void onResume() { // TODO Auto-generated method stub super.onResume(); } @Override public void onPause() { // TODO Auto-generated method stub super.onPause(); } //实现数据传递 public void getString(Callback callback) { String msg = editText.getText().toString(); callback.getString(msg); } //创建接口 public interface Callback { public void getString(String msg); } }
<RelativeLayout 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="200dp" android:layout_height="match_parent" android:background="#cbcbcb" android:orientation="vertical" > </LinearLayout> <LinearLayout android:id="@+id/right" android:layout_width="200dp" android:layout_height="match_parent" android:orientation="vertical" > </LinearLayout> <EditText android:id="@+id/editText2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentTop="true" android:layout_marginTop="14dp" android:layout_toRightOf="@+id/left" android:ems="10" android:inputType="textPassword" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/editText2" android:layout_toRightOf="@+id/left" android:text="得到Fragment的数据" /> </RelativeLayout>
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <EditText android:id="@+id/editText1" android:layout_width="match_parent" android:layout_height="wrap_content" android:inputType="text" android:ems="10" > <requestFocus /> </EditText> <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="得到Activity的数据" /> </LinearLayout>