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 ...
随机推荐
- js数组中indesOf方法的使用
<html> <head> <title>数组的操作</title> <script type="text/javascript&quo ...
- 做报表需要知道的基本的SQL语句
为客户做报表需要操作数据库,基本的SQL是必须要掌握的,下面介绍做报表最常用的SQL语句. 方法/步骤 1 查询数据:编号表示查询顺序. (8) select (9) distinct (11 ...
- Maven的scope的值
Maven的依赖范围 在pom.xml文件中,有个元素是scope,用来表示依赖的范围.之所以会有依赖范围,是因为Maven在编译.测试和运行项目时会各自使用一套classpath,依赖范围就是用来控 ...
- 使用 SSH 和 SFTP 协议
通过 SSH 和 SFTP 协议,我们能够访问其他设备,有效而且安全的传输文件等等. 几年前,我决定配置另外一台电脑,以便我能在工作时访问它来传输我所需要的文件.要做到这一点,最基本的一步是要求你的网 ...
- 为自己的网站添加Markdown功能 markedjs
Markdown几个简单的标记可以实现轻量级的代替Word方案 不多说,引入开源库js https://github.com/chjj/marked使用方式简单,如下实例代码: <!DOCTYP ...
- 来了解一下Ajax是什么?Ajax的原理?Ajax与传统Web比较?Ajax的优缺点?Ajax的Post与Get比较
一.什么是Ajax Ajax(Asynchronous Java and XML的缩写)是一种异步请求数据的web开发技术,对于改善用户的体验和页面性能很有帮助.简单地说,在不需要重新刷新页面的情况下 ...
- Jquery 数组与字符串之间的转换
var auth_list = []; $("input[name='auth_list']:checkbox").each(function () { if ($(this).a ...
- tomcat应用org.apache.catalina.LifecycleException: Failed to stop component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[]]异常的根本原因
早上,有个应用又挂了,客户端打开时报404,看进程是还在的.倒回第一次异常的地方,可见catalina.out中有如下信息: 08:46:56.646 [ContainerBackgroundProc ...
- "1130-host ... is not allowed to connect to this MySql server"登录失败
原因: 该用户没有远程连接权限. 解决:授权! mysql>GRANT ALL PRIVILEGES ON *.* TO 'user'@'%' IDENTIFIED BY 'password' ...
- 20145127《java程序设计》第四周学习总结
教材学习内容总结 第六章 继承与多态 6.1 何为继承 0.面向对象中,子类继承父类,避免城府的行为定义.正确判断使用继承的时机,以及继承之后如何活用多态,才是学习继承时的重点. 1.继承:避免多个类 ...