Android(Fragment和Activity之间通信)
Fragment的使用可以让我们的应用更灵活的适配各种型号的安卓设备,但是对于Fragment和Activity之间的通信,很多朋友应该比较陌生,下面我们就通过一个实例来看一看如何实现。
一、Activity->Fragment传递数据
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:id="@+id/rl_fragment"
- android:orientation="vertical"
- android:layout_height="match_parent" >
- <EditText
- android:id="@+id/et_input"
- android:layout_width="match_parent"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/btn_send"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="发送" />
- </LinearLayout>
2.fragment.xml:
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <TextView
- android:id="@+id/tv_fragment"
- android:layout_height="wrap_content"
- android:layout_width="wrap_content"
- />
- </LinearLayout>
3.MainActivity.java:
- package com.example.fragementcommunication;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity {
- private EditText mMainActivityET;
- private Button mSendButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- mMainActivityET = (EditText) findViewById(R.id.et_input);
- mSendButton = (Button) findViewById(R.id.btn_send);
- mSendButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String text = mMainActivityET.getText().toString();
- MyFragment myFragment = new MyFragment();
- Bundle bundle = new Bundle();
- bundle.putString("input", text);
- myFragment.setArguments(bundle);// 传递string
- FragmentManager manager = getFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.add(R.id.rl_fragment, myFragment, "myfragment");
- transaction.commit();
- Toast.makeText(MainActivity.this, "向Fragment发送数据" + text, Toast.LENGTH_SHORT).show();
- }
- });
- }
- }
4.MyFragment.java:
- package com.example.fragementcommunication;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MyFragment extends Fragment {
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragmet, null);
- TextView mFragmentTextView = (TextView) view.findViewById(R.id.tv_fragment);
- String string = getArguments().getString("input");// 获取数据;
- mFragmentTextView.setText(string);
- Toast.makeText(getActivity(), "成功获取数据" + string, Toast.LENGTH_SHORT).show();
- return view;
- }
- }
总结:发送数据方法->setArguments(bundle)
二、Fragment向Activity传值(接口回调的方式)
- package com.example.fragementcommunication;
- import com.example.fragementcommunication.MyFragment.MyListener;
- import android.app.Activity;
- import android.app.FragmentManager;
- import android.app.FragmentTransaction;
- import android.os.Bundle;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.view.Window;
- import android.widget.Button;
- import android.widget.EditText;
- import android.widget.Toast;
- public class MainActivity extends Activity implements MyListener{
- private EditText mMainActivityET;
- private Button mSendButton;
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- requestWindowFeature(Window.FEATURE_NO_TITLE);
- setContentView(R.layout.activity_main);
- mMainActivityET = (EditText) findViewById(R.id.et_input);
- mSendButton = (Button) findViewById(R.id.btn_send);
- mSendButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View v) {
- String text = mMainActivityET.getText().toString();
- MyFragment myFragment = new MyFragment();
- Bundle bundle = new Bundle();
- bundle.putString("input", text);
- myFragment.setArguments(bundle);// 传递string
- FragmentManager manager = getFragmentManager();
- FragmentTransaction transaction = manager.beginTransaction();
- transaction.add(R.id.rl_fragment, myFragment, "myfragment");
- transaction.commit();
- Toast.makeText(MainActivity.this, "向Fragment发送数据" + text, Toast.LENGTH_SHORT).show();
- }
- });
- }
- @Override
- public void callback(String back) {
- Toast.makeText(MainActivity.this, "获取到从Fragment中传来的数据" + back, Toast.LENGTH_SHORT).show();
- }
- }
2.MyFragment.java:
- package com.example.fragementcommunication;
- import android.app.Activity;
- import android.app.Fragment;
- import android.os.Bundle;
- import android.view.LayoutInflater;
- import android.view.View;
- import android.view.ViewGroup;
- import android.widget.TextView;
- import android.widget.Toast;
- public class MyFragment extends Fragment {
- public MyListener listener;
- private String back="已经接收到数据!谢谢";
- /**
- * 通过接口回调的方式从Fragment向activity传值;
- * @author Administrator
- *
- */
- public interface MyListener{
- public void callback(String back);
- }
- @Override
- public void onAttach(Activity activity) {
- super.onAttach(activity);
- listener=(MyListener) activity;
- }
- @Override
- public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
- View view = inflater.inflate(R.layout.fragmet, null);
- TextView mFragmentTextView = (TextView) view.findViewById(R.id.tv_fragment);
- String string = getArguments().getString("input");// 获取数据;
- mFragmentTextView.setText(string);
- Toast.makeText(getActivity(), "成功获取数据" + string, Toast.LENGTH_SHORT).show();
- Toast.makeText(getActivity(), "向Activity传递数据" + back, Toast.LENGTH_SHORT).show();
- listener.callback(back);
- return view;
- }
- }
运行如下:
Android(Fragment和Activity之间通信)的更多相关文章
- Android Fragment与Activity之间的数据交换(Fragment从Activity获取数据)
Fragment与Activity之间的数据交换,通常含有3: 一.Fragment从Activity获取数据(仅本文介绍了一个第一): 两.Activity从Fragment获取数据: 三.Frag ...
- android中fragment与activity之间通信原理以及例子
参考文章 http://blog.csdn.net/guozh/article/details/25327685#comments Activity和fragment通信方式一般有3种方法 1.在fr ...
- Android Service与Activity之间通信的几种方式
在Android中,Activity主要负责前台页面的展示,Service主要负责需要长期运行的任务,所以在我们实际开发中,就会常常遇到Activity与Service之间的通信,我们一般在Activ ...
- Fragment 和 Activity 之间通信
在 Activity 中获取 Fragment 实例: FragmentManager 提供了一个类似于 findViewById 的方法,专门用于从布局文件中获取 Fragment 实例: //通过 ...
- Android Service与Activity之间通信
主要分为: 通过Binder对象 通过broadcast(广播)的形式 Activity调用bindService (Intent service, ServiceConnection conn, i ...
- Android系列之Fragment(三)----Fragment和Activity之间的通信(含接口回调)
[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...
- Android 笔记-Fragment 与 Activity之间传递数据
Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...
- 适配器(adapter)与fragment之间、fragment与activity之间的通信问题
一.适配器(adapter)与fragment之间通信 通过本地广播进行通信 步骤如下 在adapter中代码 声明本地广播管理 private LocalBroadcastManager local ...
- 安卓Fragment和Activity之间的数据通讯
Fragment是Android3.0之后才推出来的.可以用来做底部菜单,现在很多APP都有用到这个底部菜单.当然TabHost也可以用来做底部菜单,但是Fragment来做,动画效果这些可以做得更炫 ...
随机推荐
- Google Guice 之绑定1
绑定和依赖注入区别 绑定,使用时 需要通过 injector 显示获取 依赖注入,只需要显示获取主类,他的依赖是通过@Injector 和 绑定关系 隐式注入的 http://blog.csdn.ne ...
- Go语言基础单元测试示例
这个要熟悉原理,要能写.. 但现在..... 注意,没有main函数,以_test.go结尾,命令go test -v package main import ( "testing" ...
- 167. Two Sum II - Input array is sorted【Easy】【双指针-有序数组求两数之和为目标值的下标】
Given an array of integers that is already sorted in ascending order, find two numbers such that the ...
- Java-多线程编程(一)
创建多线程 [Thread class]1.继承Thread类,重写run() [Runnable接口]2.实现Runnable接口,重写run() [*Callable接口]3.实现Callable ...
- python基础day4
1.列表生成式,迭代器&生成器 列表生成式 将列表[0,1,2,3,4,5,6,7,8]中的每个值加1,如何实现?常用的几种方法 方法一: a=[0,1,2,3,4,5,6,7,8] for ...
- Xamarin Android SDK无法更新的解决办法
Xamarin Android SDK无法更新的解决办法 Xamarin Android SDK无法更新的解决办法,更新时候,提示警告信息:A folder failed to be moved. ...
- 【SPOJ 220】Relevant Phrases of Annihilation
http://www.spoj.com/problems/PHRASES/ 求出后缀数组然后二分. 因为有多组数据,所以倍增求后缀数组时要特判是否越界. 二分答案时的判断要注意优化! 时间复杂度\(O ...
- JZYZOJ 1360 [usaco2011feb]人品问题 DP 树状数组 离散化
http://172.20.6.3/Problem_Show.asp?id=1360 好想好写 代码 #include<iostream> #include<cstdio&g ...
- Python学习札记
Python是很多公司都在使用的一种脚本语言,其语法与Perl.C++.JAVA等都大同小异.本文仅对一些比较常用的语法结构进行总结,比如字典.列表.正则匹配.读写文件等.供广大喜爱Python的同学 ...
- codevs 1349 板猪的火车票
1349 板猪的火车票 时间限制: 1 s 空间限制: 128000 KB 题目等级 : 黄金 Gold 题目描述 Description 奸商zn(请勿对号入座)开办了一家火车公司,弱弱的板猪 ...