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来做,动画效果这些可以做得更炫 ...
随机推荐
- 遇见Python.h: No such file or directory的解决方法
出现No such file or directory的错误,有两种情况,一种是没有Python.h这个文件,一种是Python的版本不对, 可以进入/usr/include/文件夹下的Pythonx ...
- 把web项目部署到tomcat上
首先在服务器搭建JDK环境:https://www.cnblogs.com/lb809663396/p/5855877.html 然后把tomcat文件包复制到服务器上,访问http://localh ...
- coercing to Unicode错误的一个解决办法
http://blog.csdn.net/happen23/article/details/46683813
- java网络通信:HTTP协议
熟悉java的同学,都知道javaEE当中很大一部分的内容,是WEB开发,那么在进行WEB开发的学习过程当中,除了需要学习SpringMVC/STRUTS2等框架以外,我们还需要对在WEB开发当中常用 ...
- MD5加密和RSA加密
1.MD5加密 MD5(单向散列算法)的全称是Message-Digest Algorithm 5(信息-摘要算法),MD5算法的使用不需要支付任何版权费用. MD5的功能: ①.输入任意长 ...
- Word Ladder(LintCode)
Word Ladder Given two words (start and end), and a dictionary, find the length of shortest transform ...
- Nginx开启跨域访问
CORS on Nginx The following Nginx configuration enables CORS, with support for preflight requests. # ...
- ActiveMQ 认证(一)
新搭建的ActiveMQ服务,在发布和读取消息时,连接的权限为ActiveMQConnection.DEFAULT_USER和ActiveMQConnection.DEFAULT_PASSWORD. ...
- CSS 笔记——盒子模型
2. 盒子模型 在CSS眼中,任何HTML标签对象都是一个矩形,有长度,宽度以及各角的定位坐标,俗称CSS 盒子模型理论. Margin(外边距) - 清除边框外的区域,外边距是透明的. Border ...
- hdu 1965 (莫比乌斯函数 莫比乌斯反演)
GCD Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submiss ...