Android : Fragment 传递数据 — 简单应用

news2024/9/25 1:22:19

示例图:

创建 Fragment  new -> Fragment -> Fragment(Blank)

MainActivity.java

package com.example.fragmentdemo;

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

import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.TextView;

public class MainActivity extends AppCompatActivity {

    private TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = findViewById(R.id.tv_get_result);

        TwoFragment twoFragment = new TwoFragment();
        //数据传递
        Bundle bundle = new Bundle();
        bundle.putString("name","张三丰");
        twoFragment.setArguments(bundle);

        //接收TwoFragment 传递过来的参数
        twoFragment.setPassingData(new TwoFragment.Myinterface() {
            @Override
            public void getResult(String data) {
                textView.setText(data);
            }
        });

        //动态添加Fragment
        FragmentManager fragmentManager = getSupportFragmentManager();
        FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
          // 通过id , 把 twoFragment 添加到LinearLayout布局中
        fragmentTransaction.add(R.id.linear_layout,twoFragment);
        // 替换
//        fragmentTransaction.replace(R.id.linear_layout,twoFragment);
        
        //删除
//        fragmentTransaction.remove(twoFragment);
        // 添加到回退栈  点击返时重新创建fragment  
       fragmentTransaction.addToBackStack(null);
       
        //提交
        fragmentTransaction.commit();
    }
}

HomeFragment.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;


public class HomeFragment extends Fragment {


    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_home, container, false);
    }
}

TwoFragment.java

package com.example.fragmentdemo;

import android.os.Bundle;

import androidx.fragment.app.Fragment;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;


public class TwoFragment extends Fragment {
    private TextView textView;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_two, container, false);
        //获取数据
        String name = getArguments().getString("name");
        textView = view.findViewById(R.id.tv_get_data);
        textView.setText(name);

        return view;
    }

    // 给主页面传递参数
    public void setPassingData(Myinterface myinterface){
        String name = "周芷若";
        myinterface.getResult(name);
    }
    //定义一个接口
    public interface Myinterface{
        void getResult(String data);
    }
}

主布局 activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">


    <TextView
        android:textSize="24sp"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Fragment 简单应用"
        />
    <TextView
        android:id="@+id/tv_get_result"
        android:textSize="24sp"
        android:textColor="#ff00ff00"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
 />
    <!-- 注意:要加id
      android:name 引入java类-->
    <fragment
        android:id="@+id/fragment_test"
        android:name="com.example.fragmentdemo.HomeFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        />

    <LinearLayout
        android:id="@+id/linear_layout"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:orientation="horizontal" />
</LinearLayout>

fragment_home.xml

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:background="#00BCD4"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".HomeFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:text="@string/hello_blank_fragment" />


</FrameLayout>

fragment_two.xml

<?xml version="1.0" encoding="utf-8"?>
<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"
    android:background="#ff00ff"
    tools:context=".TwoFragment">

    <!-- TODO: Update blank fragment layout -->
    <TextView
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello_blank_fragment_two" />
    <TextView
        android:text="获取到的数据是:"
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />
    <TextView
        android:id="@+id/tv_get_data"
        android:textSize="24sp"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        />

</LinearLayout>

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/1263441.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

媒介盒子分享:实用软文写作技巧

信息洪流中品牌方如果想要抓住用户注意力就需要一定的技巧&#xff0c;而品牌方写软文时是不是经常面临无法与用户产生共鸣、软文没有转化率的困境。今天媒介盒子就来和大家分享&#xff1a;实用软文写作技巧。 一、 强化优势 人一定是会往自己喜欢的方向走&#xff0c;远离不…

杂记 | 使用Docker安装并配置MongoDB以支持事务(单副本,并解决了证书文件错误的问题)

文章目录 00 安装前的准备01 创建Docker Compose文件02 设置证书文件03 启动MongoDB04 初始化副本集和创建用户05 验证安装 00 安装前的准备 在开始之前&#xff0c;确保已经安装了Docker&#xff0c;本文基于Docker Compose进行示范&#xff0c;没有装Docker Compose也可将其…

深度学习之图像分类(十五)DINAT: Dilated Neighborhood Attention Transformer理论精简摘要(二)

Dilated Neighborhood Attention Transformer摘要 局部注意力机制&#xff1a;例如滑动窗口Neighborhood Attention&#xff08;NA&#xff09;或Swin Transformer的Shifted Window Self Attention。 优点&#xff1a;尽管在降低自注意力二次复杂性方面表现出色&#xff0c; …

Java精品项目源码基于SpringBoot的智慧园区管理系统(v67)

Java精品项目源码基于SpringBoot的智慧园区管理系统(v67) 大家好&#xff0c;小辰今天给大家介绍一个智慧园区管理系统&#xff0c;演示视频公众号&#xff08;小辰哥的Java&#xff09;对号查询观看即可 文章目录 Java精品项目源码基于SpringBoot的智慧园区管理系统(v67)难度…

【NI-RIO入门】为CompactRIO供电

在大多数情况下&#xff0c;您可以使用可直接连接系统的电源&#xff0c;例如墙上的电源插座。但是&#xff0c;某些应用程序或环境缺乏可用电源&#xff0c;您必须使用其他电源&#xff0c;例如电池。无论您是否有可用电源&#xff0c;您可能都希望通过为系统提供一些冗余来确…

ZYNQ_project:IIC_EEPROM

EEPROM简介&#xff1a; EEPROM(Electrically Erasable Progammable Read Only Memory&#xff0c; E2PROM)是指带电可擦可编程只读存 储器&#xff0c;是一种常用的非易失性存储器&#xff08;掉电数据不丢失&#xff09;&#xff0c; E2PROM 有多种类型的产品&#xff0c;我…

为什么我不能给shopify的图片添加alt

首先我们要明白是什么ALT标签&#xff0c;为什么要添加这个标签&#xff0c;这个标签有什么用 ALT标签是什么 ALT属性是HTML的一部分&#xff0c;它为那些无法查看图像的用户提供替代的文本描述。 ALT标签有什么用 使用ALT属性还可以帮助搜索引擎爬虫更好地理解您的网站内容。有…

npm管理发布包-创建与发布

创建与发布 我们可以将自己开发的工具包发布到 npm 服务上&#xff0c;方便自己和其他开发者使用&#xff0c;操作步骤如下 创建文件夹&#xff0c;并创建文件indexjs&#xff0c;在文件中声明函数&#xff0c;使用 module.exports 暴露npm初始化工具包&#xff0c;package.j…

LeetCode Hot100 42.接雨水

题目&#xff1a; 给定 n 个非负整数表示每个宽度为 1 的柱子的高度图&#xff0c;计算按此排列的柱子&#xff0c;下雨之后能接多少雨水。 方法一&#xff08;相向双指针&#xff09;&#xff1a;竖着计算面积 代码&#xff1a; class Solution {public int trap(int[] he…

docker 安装elasticsearch集群

准备工作 docker 安装好&#xff0c;docker compose 安装好编辑好docker-compose.yml文件&#xff08;本文会提供&#xff09;生成elastic-certificates.p12密钥&#xff0c;与docker-compose文件在同一个目录&#xff08;本文会介绍生成方式&#xff09;准备elasticsearch配置…

浅谈Python中的鸭子类型和猴子补丁

文章目录 前言一、鸭子类型二、猴子补丁关于Python技术储备一、Python所有方向的学习路线二、Python基础学习视频三、精品Python学习书籍四、Python工具包项目源码合集①Python工具包②Python实战案例③Python小游戏源码五、面试资料六、Python兼职渠道 前言 Python 开发者可能…

2020年1月31日 Go生态洞察:pkg.go.dev的未来步骤

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

如何运用AppLink平台中的数据连接器组件

AppLink平台组件组成 AppLink平台组件分成三个板块触发事件组件、基础组件和数据连接器 数据连接器组件里面有10个组件&#xff0c;目前也在不断新增更多的数据连接器&#xff0c;那他们在AppLink平台里的原理、触发动作以及怎么使用呢&#xff1f;接下来用MySQL和TimescaleD…

Text-to-SQL小白入门(九)InstructGPT论文:教你如何训练ChatGPT

论文概述 InstructGPT和ChatGPT 的训练流程基本一致 &#xff0c;ChatGPT是改进后的InstructGPT&#xff0c;比如InstructGPT是基于GPT-3训练&#xff0c;而ChatGPT是基于GPT-3.5训练。 基本信息 英文标题&#xff1a;Training language models to follow instructions with h…

【鸿蒙应用ArkTS开发系列】- 选择图片、文件和拍照功能实现

文章目录 前言创建多媒体Demo工程创建MediaBean 实体类创建MediaHelper工具类API标记弃用问题动态申请多媒体访问权限实现选择图片显示功能打包测试 前言 在使用App的时候&#xff0c;我们经常会在一些社交软件中聊天时发一些图片或者文件之类的多媒体文件&#xff0c;那在鸿蒙…

当TinyMCE富文本编辑器遇到Vue3+nuxt+ts项目,分享引入成功案例及过程中踩的那些坑

文章目录 前言遇到的坑插入上传图片插件上传图片请求与返回值处理本地文件引入报错解决源码 前言 如果你的前端项目技术栈使用的是Vue3nuxtts&#xff0c;并且老大让你集成一下那个传说中非常丝滑的TinyMCE富文本编辑器&#xff0c;那么恭喜你和我一样中大奖了。 网上找了好久…

uniapp 导航分类

商品分类数据&#xff0c;包括分类名称和对应的商品列表点击弹出 列表的内容 展示效果如下&#xff1a; 代码展示 ①div部分 <view class"container"><view class"menu-bar"><view class"menu"><view class"menu-sc…

CSDN最新最全python+pytest接口自动化(12)-自动化用例编写思路 (使用pytest编写一个测试脚本)

经过之前的学习铺垫&#xff0c;我们尝试着利用pytest框架编写一条接口自动化测试用例&#xff0c;来厘清接口自动化用例编写的思路。 我们在百度搜索天气查询&#xff0c;会出现如下图所示结果&#xff1a; 接下来&#xff0c;我们以该天气查询接口为例&#xff0c;编写接口测…

C语言——字符函数和字符串函数(上)

在编程的过程中&#xff0c;我们经常要处理字符和字符串&#xff0c;为了方便操作字符和字符串&#xff0c;C语⾔标准库中提供了⼀系列库函数&#xff0c;接下来我们就学习⼀下这些函数。 一、 字符分类函数 C语⾔中有⼀系列的函数是专⻔做字符分类的&#xff0c;也就是⼀个字…

[含泪解决]OSError: [Errno 99] Cannot assign requested address__踩坑记录——app.py绑定IP失败

踩坑记录下。 是这个样子的&#xff0c;前几天帮别人部署Python的Flask项目到云服务器上&#xff0c;然后在 app.run(host"xxx.xxx.xxx.xxx",port8080) 这行代码中&#xff0c;xxx.xxx.xxx.xxx代表我的IP地址&#xff0c;port代表我的端口号。 然后不是要部署到服…