Activty与Fragment交互通信

本文介绍如何在Android应用中通过Fragment传递参数。主要讲解了如何在Android 3.0及以下版本使用Fragment,以及如何利用Bundle对象实现Activity向Fragment传递字符串参数。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

首先,如果你想在android3.0及以下版本使用fragment,你必须引用android-support-v4.jar这个包

然后你写的activity不能再继承自Activity类了,而是要继承android.support.v4.app.FragmentActivity,一些其他的父类也有相应的变化.

这里的包一定要统一否则会不兼容报错要么同一import android.app.Fragment;这个包

由于在android的实现机制中fragment和activity会被分别实例化为两个不相干的对象,他们之间的联系由activity的一个成员对象fragmentmanager来维护

要实现Activty参数传递给Fragment方法有很多,这里介绍通过Bundle对象传递参数给Fragment

具体代码如下

Fragment的Java代码

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

import com.xhsc.layout.relativelayout.R;


public class ParamsFragment extends Fragment {
    String mMassage;

    public ParamsFragment() {
        // Required empty public constructor
    }

    public static ParamsFragment newInstance(String message) {

        Bundle args = new Bundle();


        args.putString("MESSAGE",message);
        ParamsFragment fragment = new ParamsFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override//官方推荐在onCreate()中接收参数
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
       if(getArguments()!=null){
           Bundle bundle = getArguments();
           mMassage = bundle.getString("MESSAGE");
       }

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view =inflater.inflate(R.layout.fragment_params_layout, container, false);
        TextView textView = (TextView) view.findViewById(R.id.fragment_params_show_textview);
        textView.setText(mMassage);
        return view;
    }

}


Fragment的布局Xml代码

<FrameLayout 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.xhsc.fragment.fragmentlife.paramters.ParamsFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:id="@+id/fragment_params_show_textview"
        android:textColor="@android:color/holo_purple"
        android:textSize="20sp"
        android:gravity="center"
        android:layout_width="match_parent"
        android:layout_height="match_parent"

         />

</FrameLayout>

Activity的Java代码

import android.os.Bundle;
import android.app.Activity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

import com.xhsc.layout.relativelayout.R;

public class FragmentParamsActvity extends Activity {

    EditText mEditText;
    Button mSendFragmentMsgBtn;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.fragment_activity_params_actvity_layout);
        mSendFragmentMsgBtn = (Button) findViewById(R.id.fragment_params_send_button);
        mEditText = (EditText) findViewById(R.id.fragment_params_editText);
        mSendFragmentMsgBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
<span style="white-space:pre">		</span>//需要注意是通过FragmentManager对象来管理Fragment的,<span style="font-family: Arial; font-size: 14px; line-height: 25px;">android-support-v4包情况下是getSupportFragmentManager()</span>
                getFragmentManager().beginTransaction().add(R.id.fragment_params_framelayout,ParamsFragment.newInstance(mEditText.getText().toString())).commit();
            }
        });
    }

}


Activity的Xml布局代码

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.xhsc.fragment.fragmentlife.paramters.FragmentParamsActvity">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/fragment_params_editText"
        android:layout_alignParentTop="true"
        android:layout_alignParentLeft="true" />
    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="发送消息"
        android:id="@+id/fragment_params_send_button"
        android:layout_marginTop="21dp"
        android:layout_below="@+id/fragment_params_editText"
        android:layout_alignParentLeft="true" />
<span style="white-space:pre">	</span><--<span style="font-family: Arial; background-color: rgb(255, 0, 0);">FrameLayout</span><span style="color:#ff0000;">空布局容器占位,动态添加Fragment,需要id</span>-->
    <FrameLayout
        android:id="@+id/fragment_params_framelayout"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginTop="22dp"
        android:layout_below="@+id/fragment_params_send_button"
        android:layout_alignParentLeft="true">

    </FrameLayout>


</RelativeLayout>




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值