Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> 關於Android編程 >> Android Fragment與Activity之間的數據交換(Fragment從Activity獲取數據)

Android Fragment與Activity之間的數據交換(Fragment從Activity獲取數據)

編輯:關於Android編程

Fragment與Activity之間的數據交換,大體上包括三種:

一、Fragment從Activity獲取數據(本文章只介紹第一種);

二、Activity從Fragment獲取數據;

三、Fragment之間獲取數據。


實現效果圖:

從Activity傳遞數據到兩個Fragment中,Fragment獲取數據後,展示出來。

\

源代碼:

\

布局文件:

activity_main:



    
    

    
    
   


MyFragment1的布局文件f1:




    


MyFragment2的布局文件f2:



    


代碼文件:

MainActivity:

package com.fragmentdemo5_commute;

import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;

/**
 * 一、Fragment從Activity獲取數據。
 */
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();

		MyFragment1 fragment1 = new MyFragment1();
		Bundle bundle1 = new Bundle();
		bundle1.putString("id", "Activity發送給MyFragment1的數據");
		fragment1.setArguments(bundle1);
		transaction.replace(R.id.left, fragment1, "left");

		MyFragment2 fragment2 = new MyFragment2();
		Bundle bundle2 = new Bundle();
		bundle2.putString("id", "Activity發送給MyFragment2的數據");
		fragment2.setArguments(bundle2);
		transaction.replace(R.id.right, fragment2, "right");

		transaction.commit();
	}

}

MyFragment1:

package com.fragmentdemo5_commute;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment1 extends Fragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.f1, null);
		
		TextView textView = (TextView) view.findViewById(R.id.textView);
		Bundle bundle1 = getArguments();
		textView.setText(bundle1.getString("id"));
		return view;
	}

}

MyFragment2:


package com.fragmentdemo5_commute;

import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MyFragment2 extends Fragment {
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		View view = inflater.inflate(R.layout.f2, null);
		
		TextView textView = (TextView) view.findViewById(R.id.textView);
		Bundle bundle2 = getArguments();
		textView.setText(bundle2.getString("id"));
		return view;
	}

}

源代碼下載:

點擊下載源碼

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