Android中的Fragment ---- 06(Activity向静态加载的Fragment通信)

Activity向静态加载的Fragment进行传值的思路:

1,在Fragment中定义好   get() 和  set(..) 方法,
2,通过 FragmentManager的  findFragmentById(..) 或者是  findFragmentByTag(..)得到加载进来的Fragment
3,在Activity中调用fragment的  set(..) 方法传值过去,然后在fragment中调用 get() 获取值

注意:

---- 这里的关键是要用   FragmentManager的  findFragmentById(..) 或者是  findFragmentByTag(..)  得到加载进来的Fragment.


下面直接上代码:

MainActivity中的代码是 :
public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        /**
         * 从Activity向静态加载的Fragment中传值的方法是:
         * 1,在Fragment中定义好   get() 和  set(..) 方法,
         * 2,通过 FragmentManager的  findFragmentById(..) 或者是  findFragmentByTag(..)得到加载进来的Fragment
         * 3,在Activity中调用fragment的  set(..) 方法传值过去,然后在fragment中调用 get() 获取值
         * 
         * 注意 :
         * 	  这里的关键是要用   FragmentManager的  findFragmentById(..) 或者是  findFragmentByTag(..)  得到加载进来
         * 的Fragment.
         */
        
        FragmentManager fragmentManager =  getFragmentManager();
        MyFragment myFragment = (MyFragment) fragmentManager.findFragmentById(R.id.fragment);
        String text = "这是传递过去的值: 我爱娜娜!";
        myFragment.setText(text);//调用fragment的set()来传值过去
    }

}


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

<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="com.xulunpeng.dongtaidown.MainActivity">
    
    <fragment 
        android:layout_width="300dp"
        android:layout_height="300dp"
        android:layout_centerInParent="true"
        android:id="@+id/fragment"
        android:name="com.xulunpeng.tongxinfat.MyFragment"/>
    
</RelativeLayout>



MyFragment中的代码:

public class MyFragment extends Fragment {

	//定义好 get() 和  set(..)方法用来传值 取值
	String text = "";
	
	public String getText() {
		return text;
	}

	public void setText(String text) {
		this.text = text;
	}

	public View onCreateView(LayoutInflater inflater, ViewGroup container,
			Bundle savedInstanceState) {
		
		View view = inflater.inflate(R.layout.layou_fragment, null);
		
		final TextView textView = (TextView) view.findViewById(R.id.textView);
		Button btn = (Button) view.findViewById(R.id.btn);
		btn.setOnClickListener(new OnClickListener() {
			
			public void onClick(View v) {
				String text = getText();//获取传递过来的值
				textView.setText(text);
			}
		});
		
		return view;
	}
	
}


布局文件 layout_fragment.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"
    android:orientation="vertical" >

    <RelativeLayout 
        android:layout_width="200dp"
        android:layout_height="200dp"
        android:background="@android:color/black"
        android:layout_gravity="center">
        <TextView
            android:layout_width="180dp"
            android:layout_height="40dp"
            android:background="@android:color/white"
            android:layout_centerInParent="true"
            android:textSize="20sp"
            android:id="@+id/textView"/>
        <Button
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_centerHorizontal="true"
            android:layout_alignParentBottom="true"
            android:text="点击按钮获取值"
            android:id="@+id/btn"/>
    </RelativeLayout>
    
</LinearLayout>



点击传值前:




点击传值后:



  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值