•前言

  继上次学习了《通过 Intent 完成点击按钮实现页面跳转》后,我们知道了如何通过 Intent 实现页面跳转;

  Intent 除了可以实现页面跳转外,还可以在跳转的时候传递数据;

  接下来我们就来看看如何传递;

•准备工作

  接着使用上次的活动 MainActivity 和 AnotherActivity;

  对布局文件 activity_another.xml 做一些小小的改动;

activity_another.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"> <TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="This is Another Activity!"
android:textSize="20sp"
android:textColor="@color/black"/> <TextView
android:id="@+id/string"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/> </RelativeLayout>

  额外添加了一个 TextView 用于接收传递的数据;

•向下一个活动传递数据

  使用 Intent 不仅可以启动一个活动,还可以在启动活动的时候传递数据;

  接下来我们就看看如何传递;

  Intent 中提供了一系列 putExtra() 方法的重载;

  该方法可以把我们想要传递的数据暂存在 Intent 中;

  通过 Intent 启动了另一个活动后,只需要把这些数据从 Intent 中取出来就可以了;

  修改 MainActivity.java 中的代码;

MainActivity.java

public class MainActivity extends AppCompatActivity {

    ......

    @Override
protected void onCreate(Bundle savedInstanceState) { ...... mBtn.setOnClickListener(new View.OnClickListener(){ @Override
public void onClick(View v) {
Intent intent = new Intent(MainActivity.this,AnotherActivity.class); String data = "我是传递的数据:data";
intent.putExtra("key",data); startActivity(intent);
}
});
}
}

  通过 Intent 启动 AnotherActivity,并通过  putExtra() 方法传递了一个字符串 data;

   putExtra() 接受两个参数:

  • 第一个参数是键 key

    • 用于后面从 Intent 中取值
  • 第二个参数是值 data

    • 真正要传递的数据

  然后,我们在 AnotherActivity 中将该数据取出,并设置到 TextView 上;

AnotherActivity.java

public class AnotherActivity extends AppCompatActivity {

    private TextView mTvString;

    @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another); mTvString = findViewById(R.id.string);
Intent intent = getIntent();
String data = intent.getStringExtra("key");
mTvString.setText(data);
}
}

  首先,通过  getIntent() 方法获取到用于启动 AnotherActivity 的 Intent;

  然后调用  getStringExtra()  方法,传入相应的键值,就可以得到传递的数据了;

  通过  setText() 方法将其显示在 UI 界面上;

运行效果

  

Android 之 使用 Intent 在活动间传递数据的更多相关文章

  1. Android基础知识04—Activity活动之间传递数据

    ------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...

  2. android中使用Intent在activity之间传递数据

    android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...

  3. 使用Bundle在Activity间传递数据

    使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...

  4. 小菜学习Winform(五)窗体间传递数据

    前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...

  5. C#中使用SendMessage在进程间传递数据的实例

    原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...

  6. StoryBoard学习(5):使用segue页面间传递数据

    StoryBoard学习(5):使用segue页面间传递数据 函数: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sen ...

  7. WinForm 窗体间传递数据

    前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...

  8. 在微信小程序页面间传递数据总结

    在微信小程序页面间传递数据 原文链接:https://www.jianshu.com/p/dae1bac5fc75 在开发微信小程序过程之中,遇到这么一些需要在微信小程序页面之间进行数据的传递的情况, ...

  9. Andoid Intent学习之在各个活动之间传递数据

    Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...

随机推荐

  1. Matthew Effect

    Matthew Effect 马太效应 / 马修效应 马太效应(Matthew Effect),是指好的愈好,坏的愈坏,多的愈多,少的愈少的一种现象, 即两极分化现象. 来自于圣经<新约•马太福 ...

  2. how to install GitLab on Raspberry Pi OS

    how to install GitLab on Raspberry Pi OS GitLab & Raspberry Pi refs https://about.gitlab.com/ins ...

  3. github & markdown & image layout

    github & markdown & image layout css & right https://github.com/sindresorhus/log-symbols ...

  4. macOS finder show hidden files

    macOS finder show hidden files 显示 MacOS 上的隐藏文件和文件夹 https://zh.wikihow.com/显示Mac-OS-X上的隐藏文件和文件夹 $ def ...

  5. Xpath in JavaScript

    test html <p>title</p> <ul class="list a" id="list"> <li> ...

  6. js 动态修改页面文本字体

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  7. 启动Turtlesim,输入roscore命令,报错

    Error: Traceback (most recent call last):   File "/opt/ros/indigo/lib/python2.7/dist-packages/r ...

  8. Python学习笔记_斐波那契数列

    """ 1.生成100项斐波那契数列 2.求第n项斐波那契数列的值是多少 3.给定终止值,生成此前斐波那契数列 """ # 求第n项斐波那契 ...

  9. 2021-2-25:对于 Java MMAP,如何查看文件映射脏页,如何统计MMAP的内存大小?

    我们写一个测试程序: public static void main(String[] args) throws Exception { RandomAccessFile randomAccessFi ...

  10. 怎么去掉右下角的thinkphp的图标

    关闭thinkphp右下角的trace可以试试以下步骤: 1.在入口文件index.php 加入 define("APP_DEBUG", false); 2.在config.php ...