Activity向Fragment中传值

activity向fragment中传值   
1。要传的值  放到Bundle对象里

2。通过创建传递封Fragment 将bundle对象 通过 setArguments()传递到fragment

3.Fragment中 通过getArguments() 得到 bundle对象  就能得到里面的值



代码:

MainActivity:


[java] view plain copy
print ?
  1. package com.qianfeng.activitytofragment;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Activity;  
  5. import android.app.FragmentManager;  
  6. import android.app.FragmentTransaction;  
  7. import android.os.Bundle;  
  8. import android.view.View;  
  9. import android.widget.EditText;  
  10.   
  11. @SuppressLint("NewApi")   
  12. public class MainActivity extends Activity {  
  13.   
  14.     private EditText et1;  
  15.     private FragmentManager manager;  
  16.     private FragmentTransaction transaction;  
  17.     @Override  
  18.     protected void onCreate(Bundle savedInstanceState) {  
  19.         super.onCreate(savedInstanceState);  
  20.         setContentView(R.layout.activity_main);  
  21.           
  22.         et1 = (EditText) findViewById(R.id.et1);  
  23.           
  24.         manager = getFragmentManager();  
  25.         transaction = manager.beginTransaction();  
  26.           
  27.         transaction.add(R.id.contents, new Fragment1());  
  28.         transaction.commit();  
  29.           
  30.     }  
  31.       
  32.     //点击按钮时  进行传值  
  33.     public void onclickBtn(View v){  
  34.         switch (v.getId()) {  
  35.         case R.id.button1:  
  36.             Fragment1 fragment1 = new Fragment1();  
  37.             Bundle bundle = new Bundle();  
  38.             String strValue = et1.getText().toString().trim();  
  39.             bundle.putString("str", strValue);  
  40.             fragment1.setArguments(bundle);  
  41.             //如果transaction  commit()过  那么我们要重新得到transaction  
  42.             transaction = manager.beginTransaction();  
  43.             transaction.replace(R.id.contents, fragment1);  
  44.             transaction.commit();  
  45.             break;  
  46.   
  47.         default:  
  48.             break;  
  49.         }  
  50.     }  
  51.       
  52.       
  53.       
  54. }  
package com.qianfeng.activitytofragment;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;

@SuppressLint("NewApi") 
public class MainActivity extends Activity {

	private EditText et1;
	private FragmentManager manager;
	private FragmentTransaction transaction;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        et1 = (EditText) findViewById(R.id.et1);
        
        manager = getFragmentManager();
        transaction = manager.beginTransaction();
        
        transaction.add(R.id.contents, new Fragment1());
        transaction.commit();
        
    }
    
    //点击按钮时  进行传值
    public void onclickBtn(View v){
    	switch (v.getId()) {
		case R.id.button1:
			Fragment1 fragment1 = new Fragment1();
			Bundle bundle = new Bundle();
			String strValue = et1.getText().toString().trim();
			bundle.putString("str", strValue);
			fragment1.setArguments(bundle);
			//如果transaction  commit()过  那么我们要重新得到transaction
			transaction = manager.beginTransaction();
			transaction.replace(R.id.contents, fragment1);
			transaction.commit();
			break;

		default:
			break;
		}
    }
    
    
    
}












Fragment :


[java] view plain copy
print ?
  1. package com.qianfeng.activitytofragment;  
  2.   
  3. import android.annotation.SuppressLint;  
  4. import android.app.Fragment;  
  5. import android.os.Bundle;  
  6. import android.view.LayoutInflater;  
  7. import android.view.View;  
  8. import android.view.ViewGroup;  
  9. import android.widget.TextView;  
  10.   
  11. @SuppressLint("NewApi")   
  12. public class Fragment1 extends  Fragment{  
  13.     @Override  
  14.     public View onCreateView(LayoutInflater inflater, ViewGroup container,  
  15.             Bundle savedInstanceState) {  
  16.         // TODO Auto-generated method stub  
  17.         View view = inflater.inflate(R.layout.fragment_layout1, container, false);  
  18.         TextView textView1 = (TextView) view.findViewById(R.id.textView1);  
  19.         Bundle bundle = getArguments();//从activity传过来的Bundle  
  20.         if(bundle!=null){  
  21.             textView1.setText(bundle.getString("str"));  
  22.         }  
  23.           
  24.         return view;  
  25.     }  
  26. }  
package com.qianfeng.activitytofragment;

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

@SuppressLint("NewApi") 
public class Fragment1 extends  Fragment{
	@Override
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		View view = inflater.inflate(R.layout.fragment_layout1, container, false);
		TextView textView1 = (TextView) view.findViewById(R.id.textView1);
		Bundle bundle = getArguments();//从activity传过来的Bundle
		if(bundle!=null){
			textView1.setText(bundle.getString("str"));
		}
		
		return view;
	}
}


  

你可能感兴趣的:(fragment)