Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android開發之Fragment與Activity的數據交互通過回調機制實現(源代碼分享)

Android開發之Fragment與Activity的數據交互通過回調機制實現(源代碼分享)

編輯:關於Android編程

上一篇文章簡單介紹了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);
	}

}

main的xml



    
    

    
        
        
    

    

        
    

    

fragment的xml




    

        
    

    

這樣就大功告成了,點擊fragment的button就可以在其editText控件顯示activity的editText控件的文字,點擊activity的button就可以得到fragment的編輯控件的數據!


  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved