Android中的Fragment ---- 04(Activity和Fragment之间的通信)

从Avtivity往Fragment方向的通信的方法:


Activity ----> Fragment:

---- 在Activity中创建Bundle数据包, 并调用 Fragment 的 setArguments(Bundle bundle)方法, 然后在Fragment中接收该bundle数据包


有一点要注意的是:

 ---- 在Fragment中负责调用getArgument()进行接收数据的函数,一定要在Activity中的setArgument()之后调用, 否则将会接收不到信息。



下面直接上代码:

Activity中的代码:

public class MainActivity extends Activity {

    EditText editText = null;
	
      protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        /**
         * 从Activity中向Fragment中进行通信的方法 :
         *  Activity --> Fragment:
         *   在Activity中创建Bundle数据包, 并调用 Fragment 的 setArguments(Bundle bundle)方法
         *   然后在Fragment中接收该bundle数据包
         *   
         *   有一点要注意的是:
         *   在Fragment中负责调用getArgument()进行接收数据的函数,一定要在Activity中的setArgument()之后调用,
         *   否则将会接收不到信息。
         */
        
        editText = (EditText) findViewById(R.id.editText);
        
    }
    
    public void onClick(View view)
    {
    	//向bundle中添加信息,然后调用setArguments(..)
    	String text = editText.getText().toString()+"";
    	MyFragment myFragment = new MyFragment();
    	Bundle bundle = new Bundle();
    	bundle.putString("name", text);
    	myFragment.setArguments(bundle);//在这里发送Bundle
    	
    	//将该 Fragment加载进Activity中,当绘制Fragment上的组件时,才会调用Fragment的onCreateView(..)函数
    	//因为获取getArgument()是写在onCreateView()中,所以才要先去加载Fragment进Activity中
    	FragmentManager fragmentManager = getFragmentManager();
    	FragmentTransaction beginTransaction = fragmentManager.beginTransaction();
    	beginTransaction.add(R.id.container, myFragment, "fragment");
    	beginTransaction.commit();
    }

}



Fragment中的代码:

public class MyFragment extends Fragment {

	//每次创建时,绘制Fragment的组件时,调用该方法
	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.fragment_layout, null);
		TextView tv = (TextView) view.findViewById(R.id.tv_fragment);
		
		//在这里接收从Activity传递过来的Bundle中的数据信息
		String str = getArguments().getString("name");
		
		tv.setText("传送来的数据是 : " + str);
		return view;
	}
	
}



布局文件Activity_main.xml中的代码:

<LinearLayout 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="com.xulunpeng.fragmenttongxin.MainActivity"
    android:orientation="vertical">
    
    <EditText
        android:layout_width="300dp"
        android:layout_height="50dp"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="20dp"
        android:id="@+id/editText"/>
    
    <Button 
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="将数据传送到Fragment上"
        android:layout_gravity="center_horizontal"
        android:onClick="onClick"/>
    
    <LinearLayout 
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:orientation="vertical"
        android:id="@+id/container">
        
    </LinearLayout>
    
</LinearLayout>



布局文件fragment_layout.xml中的代码:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
    
    <RelativeLayout 
        android:layout_width="150dp"
        android:layout_height="150dp"
        android:background="@android:color/black"
        android:layout_centerInParent="true">
        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="这是个Fragment"
            android:textColor="@android:color/white"
            android:layout_centerInParent="true"
            android:textSize="20sp"
            android:id="@+id/tv_fragment"/>
        
    </RelativeLayout>
    
</RelativeLayout>




效果图:

点击传送数据前    和    传送了数据后的效果图:





总结 :

----  就是在 Activity中通过 setArguments(..)传个 Bundle过去,然后在Fragment中调用getArguments()来接受该bundle
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值