Fragment之间的交互

- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#00FF00"
- >
- <TextView
- android:id="@+id/lblFragment1"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is fragment #1" />
- </LinearLayout>
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="#FFFE00"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="This is fragment #2" />
- <Button android:id="@+id/btnGetText"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Get text in Fragment #1" />
- </LinearLayout>
- package net.learn2develop.Fragments;
- import net.learn2develop.Fragments.R;
- import android.app.Activity;
- import android.os.Bundle;
- public class MainActivity extends Activity {
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- }
- }
- package net.learn2develop.Fragments;
- import android.app.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 android.widget.Toast;
- public class Fragment2 extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater,
- ViewGroup container, Bundle savedInstanceState) {
- // Inflate the layout for this fragment
- return Inflater.inflate(R.layout.fragment2,
- container, false);
- }
- @Override
- public void onStart() {
- super.onStart();
- //---Button view---
- Button btnGetText = (Button)
- getActivity().findViewById(R.id.btnGetText);
- btnGetText.setOnClickListener(new View.OnClickListener() {
- public void onClick(View v) {
- TextView lbl = (TextView)
- getActivity().findViewById(R.id.lblFragment1);
- Toast.makeText(getActivity(), lbl.getText(),
- Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="horizontal"
- android:layout_width="match_parent"
- android:layout_height="match_parent">
- <fragment
- android:name="net.learn2develop.Fragments.Fragment1"
- android:id="@+id/fragment1"
- android:layout_weight="1"
- android:layout_width="0px"
- android:layout_height="match_parent" />
- <fragment
- android:name="net.learn2develop.Fragments.Fragment2"
- android:id="@+id/fragment2"
- android:layout_weight="1"
- android:layout_width="0px"
- android:layout_height="match_parent" />
- </LinearLayout>

示例说明
- TextView lbl = (TextView)
- getActivity().findViewById(R.id.lblFragment1);
- Toast.makeText(getActivity(), lbl.getText(),
- Toast.LENGTH_SHORT).show();
Fragment之间的交互的更多相关文章
- 安卓任意两个或多个Fragment之间的交互与刷新界面
平时项目中遇到一个问题:在子fragment中刷新父fragment的界面,通俗的说也就是在任何一个fragment中来刷新另一个fragment.大家都知道activity和fragment之间的交 ...
- Fragments之间的交互(实现参数传递)
Fragments之间的交互(实现参数传递) 日常开发中,通常Fragments之间可能需要交互,比如基于用户事件改变Fragment的内容.所有Fragment之间的交互需要通过他们关联的Activ ...
- Android中Fragment与Activity之间的交互(两种实现方式)
(未给Fragment的布局设置BackGound) 之前关于Android中Fragment的概念以及创建方式,我专门写了一篇博文<Android中Fragment的两种创建方式>,就如 ...
- Android中使用开源框架EventBus3.0实现Fragment之间的通信交互
1.概述 在之前的博文中简单介绍过如何实现fragment之间的信息交互:<Android中Fragment与Activity之间的交互(两种实现方式)>,今天继续给大家介绍一种可以实现此 ...
- fragment之间的信息交互——onActivityResult()不经过Activity
1.本文讲述如何fragment与fragment之间互传信息,不用使用Activity的onActivityResult()方法 核心思想:FirstFragment获取到SecondFragmen ...
- Android学习路径(23)应用Fragment建立动态UI——Fragment之间的通信
为了要重用Fragment的UI组件.你应该为它们每个都构建一个完整独立的,模块化的组件来定义他自身的布局和行为. 一旦你定义了这些可重用的Fragments.你能够通过activity关联它们同一时 ...
- Android基础——Fragment与Activity交互
今天继续讲解Fragment组件的特性,主要是跟Activity的交互和生命周期的关系,我们前面已经说过Fragment是依赖于Activity的,而且生命周期也跟Activity绑定一起.下面我们看 ...
- 我的Android第五章:通过Intent实现活动与活动之间的交互
Intent在活动的操作 作用: Itent是Android程序中各个组件直接交换的一个重要方式可以指定当前组件要执行任务同时也可以给各个组件直接进行数据交互 同时Inten ...
- Fragment之间的通信(四)
自定义两个fragment的布局和java类. 在mainactivity中引用布局文件 在其中的一个fragment中的控件上添加监听,获取到另一个fragment中控件的内容,展示出来完成frag ...
随机推荐
- jmeter 读取excel数据
jmeter 读取excel数据使用的方法是使用Jmeter CSV Data Set Config参数化 但是将excel文件保存成csv格式后,jmeter读取后返回的数据总是出现乱码问题, 以下 ...
- 单利模式及python实现方式
单例模式 单例模式(Singleton Pattern)是一种常用的软件设计模式,该模式的主要目的是确保某一个类只有一个实例存在.当希望在整个系统中,某个类只能出现一个实例时,单例对象就能派上用场. ...
- Linux系统故障排查和修复技巧
本文转自:http://os.51cto.com/art/201304/388384.htm 我发现Linux系统在启动过程中会出现一些故障,导致系统无法正常启动,我在这里写了几个应用单用户模式.GR ...
- 网站app原型设计工具:axure,Mockups,墨刀
网站app原型设计工具:axure,Mockups,墨刀 Balsamiq Mockups 3 网站原型设计工具非常高效,非常简单,几分钟就能搞定比axure好用很多 墨刀 - 免费的移动应用原型与线 ...
- printf("%d",5.01)和printf("%f",5)的输出结果
printf(); printf("%d\n",5.01); printf(); printf(.f); 输出结果: 看到结果,会感觉非常奇怪.1处怎么会输出0呢?2又为何会显示这 ...
- Python入门之面向对象之类继承与派生
本章内容 一.继承 二.抽象类 三.继承的实现原理 ======================================================= 一.继承 1. 继承的定义 继承是一 ...
- Redis计算地理位置距离-GeoHash
Redis 在 3.2 版本以后增加了地理位置 GEO 模块,意味着我们可以使用 Redis 来实现摩拜单车「附近的 Mobike」.美团和饿了么「附近的餐馆」这样的功能了. 地图元素的位置数据使用二 ...
- 08: 查看Linux系统基本信息和硬盘CPU等
目录: 1.1 查看Linux系统基本信息 1.2 查看三秒内的平均CPU 1.3 查看内存使用情况 1.4 查看当前系统负载 1.1 查看Linux系统基本信息返回顶部 1.查看Linux系统uui ...
- tf.truncated_normal的用法
tf.truncated_normal(shape, mean, stddev) :shape表示生成张量的维度,mean是均值,stddev是标准差.这个函数产生正太分布,均值和标准差自己设定.这是 ...
- SQL数据插入字符串时转义函数
函数一: std::string CheckString(std::string& strSource) { std::string strOldValue = "'"; ...