Android+Fragment与Activity之间的信息传递——笔记4

通过Java的面向对象传递信息;

1、建立一个接口

复制代码
package com.example.fragment;

public interface ITFragm {
    void sendmsgToAct(String msg);

      default String  getmsgFromAct() {
        return null;
    }
}

2、写一个Fragment经行信息交流

复制代码
package com.example.fragment;

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

import androidx.fragment.app.Fragment;

/**
 * A simple {@link Fragment} subclass.
 * Use the  factory method to
 * create an instance of this fragment.
 */
public class BlankFragment2 extends Fragment {
    private  View root;
    public TextView tv;
    public Button btn1,btn;
    private ITFragm itFragm1;

    /**
     * 该函数是一个公共的void类型函数,
     * 名为setITFragm,接受一个参数itFragm2,
     * 其类型为ITFragm。
     * 函数的作用是将传入的itFragm2对象赋值给类中的itFragm1成员变量。
     * 为匿名内部类做准备
     * @param itFragm2
     */
    public void setITFragm(ITFragm itFragm2){
        itFragm1=itFragm2;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        if(root==null){
             root=inflater.inflate((R.layout.fragment_blank1),container, false);
        }
        tv=root.findViewById(R.id.tv);
        btn1=root.findViewById(R.id.btn1);
        btn1.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                itFragm1.sendmsgToAct("你好,我是Frag");
                tv.setText("发送成功");//发送信息给Activity
            }
        });
        btn=root.findViewById(R.id.btn);
        btn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View view) {
                String msg= itFragm1.getmsgFromAct();
                tv.setText(msg);//接收到来自Activity的信息
            }
        });
        return root;
    }
}

3、主界面

复制代码
package com.example.fragment;

import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;

import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends AppCompatActivity implements View.OnClickListener{
  Button btn2,btn3;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        btn2=findViewById(R.id.btn2);
        btn3=findViewById(R.id.btn3);
        btn2.setOnClickListener(this);
        btn3.setOnClickListener(this);
    }
    //fragment与Activity之间通过Bundle传递数据
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.btn2:
                Bundle bundle=new Bundle();
                bundle.putString("name","张三");
                BlankFragment1 f1=new BlankFragment1();
                f1.setArguments(bundle);
                replacefragment(f1);
                break
                ;
//btn3通过面向对象的思想传递数据
            case R.id.btn3:
                BlankFragment2 blankFragment2=new BlankFragment2();

               blankFragment2.setITFragm(new ITFragm(){


                   @Override
                   public void sendmsgToAct(String msg) {
                       Toast.makeText(MainActivity.this,msg,Toast.LENGTH_SHORT).show();
                   }//接收来自Fragment的信息

                   @Override
                   public String getmsgFromAct() {
                       return "你好,我是Activity";
                   }//发送信息给Fragment


               });
                replacefragment(blankFragment2);
                break;
        }
    }


    /**
     * 这个函数用于在Android开发中切换Fragment。
     * 它通过获取FragmentManager和FragmentTransaction对象,
     * 使用replace方法将当前的Fragment替换为新的BlankFragment1实例,并提交该事务。
     * @param v
     */
    public void replacefragment(Fragment v){
        FragmentManager fragmentManager=getSupportFragmentManager();
        FragmentTransaction fragmentTransaction=fragmentManager.beginTransaction();
        fragmentTransaction.replace(R.id.fragment1,v);//替换
        fragmentTransaction.addToBackStack(null);//添加到后退栈
        fragmentTransaction.commit();//提交事务
    }

}

相关推荐
whysqwhw8 分钟前
OkHttp之okhttp-bom模块的分析
android
餐桌上的王子17 分钟前
Android 构建可管理生命周期的应用(二)
android
饕餮争锋1 小时前
设计模式笔记_创建型_建造者模式
笔记·设计模式·建造者模式
幽你一默1 小时前
Android 版本差异速查表(开发者视角)
android
不萌1 小时前
android 项目中的屏幕适配方案
android
幽你一默1 小时前
Android开发三分钟读懂mvc,mvp,mvvm,mvi
android
萝卜青今天也要开心1 小时前
2025年上半年软件设计师考后分享
笔记·学习
吃货界的硬件攻城狮2 小时前
【STM32 学习笔记】SPI通信协议
笔记·stm32·学习
蓝染yy2 小时前
Apache
笔记
小仙女喂得猪3 小时前
2025 源码应用: Retrofit之动态更换BaseUrl
android·android studio