在activity之间传递数据
在activity之间传递数据
一、简介

二、通过intent传递数据
1、在需要传数据的界面调用
intent.putExtra("data1", "我是fry");方法
//前面是键,后面是值,这个是string
2、在需要接受数据的界面调用bundle获取传递过来的数据即可
Bundle bundle = getIntent().getExtras();
String data1=bundle.getString("data1");
如果是string就getString(),int就getString(),什么数据类型的方法都有
3、实例
需要传数据的界面调用
intent.putExtra("data1", "我是fry");
需要接受数据的界面
Bundle bundle = getIntent().getExtras();
String data1=bundle.getString("data1");
data1里面的内容就是:"我是fry"
三、实例
结果图


原理:
在MainActivity页面创建数据,然后用intent.putExtra方法将数据传递给Activity01页面
代码
activityDataSend.MainActivity
package activityDataSend; import com.example.activityDataSend.R; import android.app.Activity;
import android.content.Intent;
import android.content.Loader;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button; public class MainActivity extends Activity{
private Button btn_openActivty;//创建一个button对象
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);//父类操作
setContentView(R.layout.activity_main);//引入名为activity_main的界面
btn_openActivty=(Button) findViewById(R.id.btn_openActivity);//找id为btn_openActivity的button
btn_openActivty.setOnClickListener(new OnClickListener() {//设置button点击监听 @Override
public void onClick(View v) {//onclick事件
// TODO Auto-generated method stub
30 Intent intent=new Intent();//初始化intent
31 intent.setClass(MainActivity.this,Activity01.class);//连接
32 //下面这句话用于intent在activity之间传输data
33 intent.putExtra("data1", "我是fry");//前面是键,后面是值,这个是string
34 intent.putExtra("data2", 0.2);//这个是double
35 //传递对象,serializable序列化方式
36 testClass1_serialize class1=new testClass1_serialize("fry1",21);
37 intent.putExtra("data3_obj", class1);
38
39 //传递对象,parcelable序列化方式
40 testClass2_parcelable class2=new testClass2_parcelable("fry2",22);
41 intent.putExtra("data4_obj", class2);
42
43 startActivity(intent);//打开activity
}
});
}
}
activityDataSend.Activity01
package activityDataSend; import com.example.activityDataSend.R; import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.widget.TextClock;
import android.widget.TextView; public class Activity01 extends Activity{
private TextView textView_ans;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity01); textView_ans=(TextView) findViewById(R.id.textView_ans);
22 Bundle bundle = getIntent().getExtras();
23 String data1=bundle.getString("data1");
24 double data2=bundle.getDouble("data2");
25 testClass1_serialize data3_obj=(testClass1_serialize) bundle.get("data3_obj");
26 testClass2_parcelable data4_obj=(testClass2_parcelable) bundle.get("data4_obj");
Log.d("MainActivity",bundle.get("data3_obj")+"\n"+bundle.get("data4_obj"));
System.out.println(bundle.get("data3_obj")+"\n"+bundle.get("data4_obj")); textView_ans.setText("data1: "+data1+"\ndata2: "+data2+"\ndata3_obj: "+data3_obj+"\ndata4_obj: "+data4_obj);
//textView_ans.setText("data1: "+data1+"\ndata2: "+data2+"\ndata4_obj: "+data4_obj);
}
}
Serializable序列化方式
activityDataSend.testClass1_serialize
package activityDataSend;
import java.io.Serializable;
public class testClass1_serialize implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public testClass1_serialize(String name, int age) {
super();
this.name = name;
this.age = age;
}
public testClass1_serialize() {
super();
}
@Override
public String toString() {
return "testClass1_serialize [name=" + name + ", age=" + age + "]";
}
}
Parcelable序列化方式
序列化差不多要自己写
反序列化也要自己写
activityDataSend.testClass2_parcelable
package activityDataSend; import android.os.Parcel;
import android.os.Parcelable; /*
* Parcelable序列化方式
* 序列化差不多要自己写
* 反序列化也要自己写
*/
public class testClass2_parcelable implements Parcelable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public testClass2_parcelable() {
super();
}
public testClass2_parcelable(String name, int age) {
super();
this.name = name;
this.age = age;
} @Override
public String toString() {
return "testClass2_parcelable [name=" + name + ", age=" + age + "]";
}
@Override
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}
/*
* 序列化
* (non-Javadoc)
* @see android.os.Parcelable#writeToParcel(android.os.Parcel, int)
*/
@Override
public void writeToParcel(Parcel dest, int flags) {//序列化
// TODO Auto-generated method stub
dest.writeString(name);
dest.writeInt(age);
}
/*
* 反序列化要自己写
*/
public static Parcelable.Creator<testClass2_parcelable> CREATOR=new Creator<testClass2_parcelable>() { @Override
public testClass2_parcelable[] newArray(int size) {
// TODO Auto-generated method stub
return null;
}
/*
* 反序列化
* (non-Javadoc)
* @see android.os.Parcelable.Creator#createFromParcel(android.os.Parcel)
*/
@Override
public testClass2_parcelable createFromParcel(Parcel source) {//反序列化
// TODO Auto-generated method stub
testClass2_parcelable class2=new testClass2_parcelable();
class2.setName(source.readString());
class2.setAge(source.readInt());
return class2;
}
};
}
在activity之间传递数据的更多相关文章
- Activity之间传递数据的方式及常见问题总结
Activity之间传递数据一般通过以下几种方式实现: 1. 通过intent传递数据 2. 通过Application 3. 使用单例 4. 静态成员变量.(可以考虑 WeakReferences) ...
- Activity之间传递数据或数据包Bundle,传递对象,对象序列化,对象实现Parcelable接口
package com.gaojinhua.android.activitymsg; import android.content.Intent; import android.os.Bundle; ...
- 28、activity之间传递数据&批量传递数据
import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android ...
- 【Android 复习】 : Activity之间传递数据的几种方式
在Android开发中,我们通常需要在不同的Activity之间传递数据,下面我们就来总结一下在Activity之间数据传递的几种方式. 1. 使用Intent来传递数据 Intent表示意图,很多时 ...
- Android 笔记-Fragment 与 Activity之间传递数据
Fragment 与 Activity之间传递数据有两种方法.一种是使用setArgument,一种是使用接口回调.以下先学习第一种方法. (1)使用setArgument方法: 为了便于理解,我在这 ...
- Android基础 -- Activity之间传递数据(bitmap和map对象)
原文:http://blog.csdn.net/xueerfei008/article/details/23046341 做项目的时候需要用到在2个activity之间传递一些数据,之前做的都是些字符 ...
- android中使用Intent在activity之间传递数据
android中intent传递数据的简单使用: 1.使用intent传递数据: 首先将需要传递的数据放入到intent中 Intent intent = new Intent(MainActivit ...
- 【Android基础】利用Intent在Activity之间传递数据
前言: 上一篇文章给大家聊了Intent的用法,如何用Intent启动Activity和隐式Intent,这一篇文章给大家聊聊如何利用Intent在Activity之间进行沟通. 从一个Activ ...
- 在不同Activity之间传递数据的四种常用方法
在Android中传递数据的方法非常多,本次介绍4中比较常用的数据传递方法: 1.通过Intent传递数据 2.通过静态变量(static)传递数据 3.通过剪贴板(Clipboard)传递数据 4. ...
随机推荐
- 查看Oracle的表中有哪些索引及其禁用索引
查看Oracle中表的索引是否存在 用user_indexes和user_ind_columns系统表查看已经存在的索引 对于系统中已经存在的索引我们可以通过以下的两个系统视图(user_indexe ...
- oracle建表设置主键自增
首先创建一张表 create table member( memberId number primary key, memberMail )not null, memberName ) not nul ...
- HI35xx平台调试笔记
1.音视频数据循环采集 a. 在sample_venc.c文件中,海思官方是把采集到的数据都保存到文件中,我们需要更改到缓存里,以便后面推送到rtsp/rtmp/hls服务端. ; i < s3 ...
- composer是php包管理工具
composer是 PHP 用来管理依赖(dependency)关系的工具.你可以在自己的项目中声明所依赖的外部工具库(libraries),Composer 会帮你安装这些依赖的库文件. MAC.L ...
- 更改vim高亮括号匹配颜色
vim括号匹配高亮显示在vim7.4版本, 默认就是开启的. 但是默认的括号匹配 高亮的颜色是浅蓝色, 在亮瞎眼的同时, 严重影响我们写代码, 最明显的感受 就是, 连续打出一对括号, 接下来不仔细看 ...
- 专项训练错题整理-nowcoder-算法
一.排序 1.快速排序在下列哪种情况下最易发挥其长处? 答案是: 被排序的数据完全无序. 在数据基本有序的情况下,会退化为冒泡排序,复杂度会退化为O(n^2). ①[因为,如果是基本有序的话, 那么每 ...
- 如何确定LDA的主题个数
本文参考自:https://www.zhihu.com/question/32286630 LDA中topic个数的确定是一个困难的问题. 当各个topic之间的相似度的最小的时候,就可以算是找到了合 ...
- python全栈开发从入门到放弃之迭代器生成器
1.python中的for循环 l = [1,2,3,4,5,6] for i in l: #根据索引取值 print(i) 输出结果: 1 2 3 4 5 6 2.iterable 可迭代的 可迭 ...
- 两种ajax的方法
两种Ajax方法 Ajax是一种用于快速创建动态网页的技术,他通过在后台与服务器进行少量的数据交换,可以实现网页的异步更新,不需要像传统网页那样重新加载页面也可以做到对网页的某部分作出更新,现在这项技 ...
- [入坑系列] Mybatis 中$与#的区别
1.理解 1 #是将传入的值当做字符串的形式,eg:select id,name,age from student where id =#{id},当前端把id值1,传入到后台的时候,就相当于 sel ...