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

示例图:

创建 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;@Overrideprotected 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() {@Overridepublic void getResult(String data) {textView.setText(data);}});//动态添加FragmentFragmentManager 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 {@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentreturn 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;@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {// Inflate the layout for this fragmentView 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"><TextViewandroid:textSize="24sp"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="Fragment 简单应用"/><TextViewandroid:id="@+id/tv_get_result"android:textSize="24sp"android:textColor="#ff00ff00"android:layout_width="match_parent"android:layout_height="wrap_content"/><!-- 注意:要加idandroid:name 引入java类--><fragmentandroid:id="@+id/fragment_test"android:name="com.example.fragmentdemo.HomeFragment"android:layout_width="match_parent"android:layout_height="300dp"/><LinearLayoutandroid: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 --><TextViewandroid: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 --><TextViewandroid:textSize="24sp"android:layout_width="match_parent"android:layout_height="wrap_content"android:text="@string/hello_blank_fragment_two" /><TextViewandroid:text="获取到的数据是:"android:textSize="24sp"android:layout_width="match_parent"android:layout_height="wrap_content"/><TextViewandroid:id="@+id/tv_get_data"android:textSize="24sp"android:layout_width="match_parent"android:layout_height="wrap_content"/></LinearLayout>

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

如若内容造成侵权/违法违规/事实不符,请联系编程知识网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

00.本地搭建 threejs 文档网站(网页版是外网比较慢)

three官网 https://threejs.org/ 下载代码 进入官网 可以选择github去下载 或者 下载压缩包 github 下载https链接地址 https://github.com/mrdoob/three.js.git git clone https://github.com/mrdoob/three.js.git安装依赖启动程序 安装依赖 npm i 或者 pnpm i 或者 …

GUI加分游戏

需求目标 这个简单的游戏窗口包含一个得分标签和一个按钮。每次点击按钮时&#xff0c;得分增加1&#xff0c;并更新得分标签的显示。 效果 源码 /*** author lwh* date 2023/11/28* description 这个简单的游戏窗口包含一个得分标签和一个按钮。每次点击按钮时&#xff0c;…

带残差连接的ResNet18

目录 1 模型构建 1.1 残差单元 1.2 残差网络的整体结构 2 没有残差连接的ResNet18 2.1 模型训练 2.2 模型评价 3 带残差连接的ResNet18 3.1 模型训练 3.2 模型评价 4 与高层API实现版本的对比实验 总结 残差网络&#xff08;Residual Network&#xff0c;ResNet&#xff09;…

YOLOv5算法进阶改进(5)— 主干网络中引入SCConv | 即插即用的空间和通道维度重构卷积

前言:Hello大家好,我是小哥谈。SCConv是一种用于减少特征冗余的卷积神经网络模块。相对于其他流行的SOTA方法,SCConv可以以更低的计算成本获得更高的准确率。它通过在空间和通道维度上进行重构,从而减少了特征图中的冗余信息。这种模块的设计可以提高卷积神经网络的性能。�…

使用STM32 HAL库驱动光电传感器的设计和优化

光电传感器在许多应用中起着重要的作用&#xff0c;例如自动计数、距离测量等。STM32微控制器和HAL库提供了丰富的功能和易于使用的接口&#xff0c;使得光电传感器的设计和优化变得更加便捷。本文将介绍如何使用STM32 HAL库驱动光电传感器的设计和优化&#xff0c;包括硬件设计…

【前端首屏加载速度优化(一) :nginx 开启gzip压缩】

开启gzip压缩前后对比&#xff1a; nginx.conf具体配置&#xff1a; server {# 启动后的端口listen 8882;# 开启gzip压缩gzip on;gzip_min_length 1k; gzip_buffers 4 16k; gzip_http_version 1.1; gzip_comp_level 6; gzip_types text/plain application/x-javascript…

链接1:编译器驱动程序

文章目录 GNU编译器示例编译 GNU编译器 GNU编译器&#xff08;GNU Compiler&#xff09;是由自由软件基金会&#xff08;Free Software Foundation&#xff0c;FSF&#xff09;开发和维护的一套编译器集合。这些编译器主要用于编译各种编程语言的源代码&#xff0c;将其转换为…

springboot 自定义starter逐级抽取

自定义starter 背景:各个组件需要引入starter 还有自己的配置风格 –基本配置原理 &#xff08;1&#xff09;自定义配置文件 导入配置可以在配置文件中自动识别&#xff0c;提示 导入依赖后可以发现提示 &#xff08;2&#xff09;配置文件实现 –让配置文件对其他模块生…

Python中的datetime库

1. datetime datetime是Python中用于处理日期和时间的类&#xff0c;它包含在datetime模块中。使用datetime类&#xff0c;我们可以创建表示特定日期和时间的对象&#xff0c;以及进行日期和时间的计算和操作。 from datetime import datetime, timedelta# 获取当前日期和时间…

工业产品3d交互展示数字云展厅更绿色环保

随着数字技术的飞速发展&#xff0c;3D全景汽车云展厅平台应运而生&#xff0c;为现代展览带来了前所未有的创新与变革。该平台以其独特的优点&#xff0c;为观众、艺术家和展商带来了全新的展览体验&#xff0c;开启了未来展览的新篇章。 首先&#xff0c;3D全景汽车云展厅平台…

Qt路径和Anaconda中QT路径冲突(ubuntu系统)

最近做一个项目需要配置QT库&#xff0c;本项目配置环境如下&#xff1a; Qt version 5 Operating system, version and so on ubuntu 20.04 Description 之前使用过anaconda环境安装过QT5&#xff0c;所以在项目中CMakeLists文件中使用find_package时候&#xff0c;默认使用An…

python 爬虫之 爬取网站信息并保存到文件

文章目录 前期准备探索该网页的HTML码的特点开始编写代码存入文件总的程序文件存储效果 前期准备 随便找个网站进行爬取&#xff0c;这里我选择的是(一个卖书的网站&#xff09; https://www.bookschina.com/24hour/62700000/ 我的目的是爬取这个网站的这个页面的书籍的名称以…