Android 之 使用 Intent 在活动间传递数据
•前言
继上次学习了《通过 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 在活动间传递数据的更多相关文章
- Android基础知识04—Activity活动之间传递数据
------活动之间传递数据------ 向下一个活动传递数据: Intent中提供了一系列的putExtra()方法,可以把数据暂存到Intent中,启动另一个活动的时候就可以取出来. 代码: (存 ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
- 使用Bundle在Activity间传递数据
使用Bundle在Activity间传递数据 源Activity public class SourceActivty extends Activity { private Intent intent ...
- 小菜学习Winform(五)窗体间传递数据
前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...
- C#中使用SendMessage在进程间传递数据的实例
原文:C#中使用SendMessage在进程间传递数据的实例 1 新建解决方案SendMessageExample 在解决方案下面新建三个项目:CopyDataStruct,Receiver和Send ...
- StoryBoard学习(5):使用segue页面间传递数据
StoryBoard学习(5):使用segue页面间传递数据 函数: - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sen ...
- WinForm 窗体间传递数据
前言 做项目的时候,winfrom因为没有B/S的缓存机制,窗体间传递数据没有B/S页面传递数据那么方便,今天我们就说下winfrom中窗体传值的几种方式. 共有字段传递 共有字段传递实现起来很方便, ...
- 在微信小程序页面间传递数据总结
在微信小程序页面间传递数据 原文链接:https://www.jianshu.com/p/dae1bac5fc75 在开发微信小程序过程之中,遇到这么一些需要在微信小程序页面之间进行数据的传递的情况, ...
- Andoid Intent学习之在各个活动之间传递数据
Intent是一种运行时绑定(run-time binding)机制,它能在程序运行过程中连接两个不同的组件.通过Intent,你的程序可以向Android表达某种请求或者意愿,Android会根据意 ...
随机推荐
- shit leetcode edge testcases
shit leetcode edge testcases Merge Intervals try "use strict"; /** * * @author xgqfrms * @ ...
- npm 安装 electron 失败的解决方案
npm 安装 electron 失败的解决方案 shit GFW npm 安装 electron 失败 解决方案 https://www.npmjs.com/package/nrm $ nrm ls ...
- github & personal access token
github & personal access token OAuth https://github.com/xgqfrms/webtrc-in-action/issues/1#issuec ...
- javascript & global event & custom event
javascript & global event & custom event new CustomEvent object let event = new CustomEvent( ...
- PWA & TWA
PWA & TWA https://www.bilibili.com/video/av68082979/ Service Worker workbox.js https://developer ...
- Nestjs 验证对象数组
route @Patch(':id') patch(@Param('id') id: string, @Body() removeEssayDto: RemoveEssayDto) { return ...
- PAUL ADAMS ARCHITECT:华州加州城市楼市竞争力居全美前列
美国房地产公司最新一份报告显示,美国楼市竞争力最高的十个城市中有八个来自华盛顿州和加利福尼亚州,主要原因在于越来越多的买家考虑迁居房价更划算的都会区.数据显示,斯潘那维65%的房屋以高于要价的价格售出 ...
- redis缓存穿透穿透解决方案-布隆过滤器
redis缓存穿透穿透解决方案-布隆过滤器 我们先来看一段代码 cache_key = "id:1" cache_value = GetValueFromRedis(cache_k ...
- 云服务器Centos7部署Tomcat服务器
目录 部署Tomcat服务器 1.安装JDK1.8 2.安装与启动tomcat 配置安全组(8080端口) 参考文章 部署Tomcat服务器 1.安装JDK1.8 JDK下载地址:https://ww ...
- Java自学第7期——异常(Exception)
1.概念: 异常 :指的是程序在执行过程中,出现的非正常的情况,终会导致JVM的非正常停止. 在Java等面向对象的编程语言中,异常本身是一个类, 产生异常就是创建异常对象并抛出了一个异常对象. Ja ...

