Intent 是Android 程序中各组件之间进行交互的一种重要方式,它不仅可以指明当前组
件想要执行的动作,还可以在不同组件之间传递数据。Intent 一般可被用于启动活动、启动
服务、以及发送广播等场景
   // A activity调用
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main); // 创建视图
setContentView(R.layout.my_layout);
// 找到对应的button来监听事件
findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AnotherAty.class);
i.putExtra("data", "hello word"); // 使用Intent来传参
startActivity(i);
}
});
System.out.println("onCreate");
}
    //B activity 通过Intent来获取值,并显示在textView上面
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent
tv = (TextView)findViewById(R.id.textView);
tv.setText(i.getStringExtra("data"));
}

// 如果数据比较多,可以通过 Bundle  数据包来传递数据

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main); // 创建视图
setContentView(R.layout.my_layout);
// 找到对应的button来监听事件
findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AnotherAty.class);
//i.putExtra("data", "hello word"); // 使用Intent来传参
Bundle b = new Bundle(); // 打包数据
b.putString("name", "chengzhier");
b.putInt("age", 2); i.putExtras(b);
startActivity(i);
}
});
System.out.println("onCreate");
} private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent
tv = (TextView)findViewById(R.id.textView); //i.getStringExtra("data")
Bundle data = i.getExtras();
String s = String.format("name=%s, age=%d", data.getString("name"), data.getInt("age"));
tv.setText(s);
}

// 传递一个对象 java 自带的 Serializable 虚拟化


// User类
public class User implements Serializable{ //让这个对象序列化
private String name;
private int age; public User(int age, String name ) {
this.age = age;
this.name = name;
} public void setAge(int age) {
this.age = age;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public String getName() {
return name;
}
} // A activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main); // 创建视图
setContentView(R.layout.my_layout);
// 找到对应的button来监听事件
findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AnotherAty.class); i.putExtra("user", new User(2, "zh")); startActivity(i);
}
});
System.out.println("onCreate");
} // B activiry
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent
tv = (TextView)findViewById(R.id.textView);
User u = (User)i.getSerializableExtra("user"); //取出来 String s = String.format("测试11name=%s, age=%d", u.getName(), u.getAge());
tv.setText(s);
}

//传递一个对象 安卓专门的Parcelable虚拟化

/**
* Created by ZhouXiaoHai on 2016/9/8.
User 类
*/
public class User implements Parcelable{ // 安卓自带的序列化
private int age;
private String name;
private String dogName; public void setDogName(String dogName) {
this.dogName = dogName;
} public String getDogName() {
return dogName;
} public User(int age, String name, String dogName) {
this.age = age;
this.name = name;
this.dogName = dogName;
} public void setAge(int age) {
this.age = age;
} public void setName(String name) {
this.name = name;
} public int getAge() {
return age;
} public String getName() {
return name;
} @Override
public int describeContents() {
return 0;
} @Override
public void writeToParcel(Parcel dest, int flags) { // 必须要写的 接口 Parcelable 的方法
// 这个一定要按变量顺序写
dest.writeInt(getAge());
dest.writeString(getName());
dest.writeString(getDogName());
} public static final Creator<User> CREATOR = new Creator<User>() {
@Override
public User createFromParcel(Parcel source) {
// 这个一定要按变量顺序写
return new User( source.readInt(), source.readString(), source.readString());
} @Override
public User[] newArray(int size) {
return new User[size];
}
};
} //A activity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// setContentView(R.layout.activity_main); // 创建视图
setContentView(R.layout.my_layout);
// 找到对应的button来监听事件
findViewById(R.id.butStartAnotherAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, AnotherAty.class); i.putExtra("user", new User(2, "zh", "旺财")); startActivity(i);
}
});
System.out.println("onCreate");
} //B activity
private TextView tv; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_another_aty); Intent i = getIntent(); //直接获取传过来的intent
tv = (TextView)findViewById(R.id.textView);
//User u = (User)i.getSerializableExtra("user");
User u = (User)i.getParcelableExtra("user"); String s = String.format("测试21name=%s, age=%d 狗的名字=%s", u.getName(), u.getAge(), u.getDogName());
tv.setText(s);
}

简单总结(小白): Serializable 比 Parcelable 使用起来方便,直接实现接口就好了,但是效率不高。  Parcelable效率高,但是需要自己写一些代码。

Intent传参数的更多相关文章

  1. intent 传参数

    一.传递List<String>和List<Integer>以下以传递List<String>为例,发送List<String>语法为:intent.p ...

  2. android选择图片或拍照图片上传到服务器(包括上传参数)

    From:http://blog.csdn.net/springsky_/article/details/8213898具体上传代码: 1.选择图片和上传界面,包括上传完成和异常的回调监听 [java ...

  3. 传参数应该用哪种形式——值、引用、指针?

    类型:C++ & Qt4,创建时间:十二月 30, 2011, 7:43 p.m. 标题无"转载"即原创文章,版权所有.转载请注明来源:http://hgoldfish.c ...

  4. Mybatis传参数

    1使用@Param注解传参数 mapper接口:public void updateUser(@Param("user")User user)throws Exception; m ...

  5. 如何给main传参数

    main 函数的参数有连个argc argcv[]  argc 是参数个数 argcv是参数的数组指针,且argcv的第一个参数是默认程序路径加程序名 给main传参数,需要在命令行启动程序时设置 如 ...

  6. mvc中多参数URL会很长,首次加载不传参数让url很短,路由规则实现方法[bubuko.com]

    如要实现列表中地址全路径“bubuko-11-2.html”,在首次进入时,使用短路径“bubuko.html”,只有再次href后才显示全路径“bubuko-11-2.html”,下面使用路由规则来 ...

  7. Chrome和Firefox浏览器执行new Date() 函数传参数得到不同结果的陷阱

    某日,同事问到关于new Date() 函数传参数,在火狐浏览器和谷歌浏览器控制台运行,会得到不同的结果,刚开始觉得不可能,后来实际操作才发现此陷阱 var date = new Date('2014 ...

  8. web service上传参数代码实例

    web service上传参数代码实例 这次做的项目用到webservice比较多,最开始在网上看的参考dome,发现都不行,后来发现安卓4.0以后有很大的不同,在做传参时,有些东西需要注意: 第一, ...

  9. spring mvc 传参数

    1.页面:(1)js传参数:location.href="${ctx }/forum/changeCtm.html?ctmId="+id; (2)将内容写在form表单里面,然后用 ...

随机推荐

  1. dba诊断之IO

    --查看占用系统io较大的session   SELECT se.sid,se.serial#,pr.SPID,se.username,se.status,se.terminal,se.program ...

  2. 150924-还是起得来床的好-HTML(CSS)

    一早醒来是9:10,这些天最晚的了,也可能是睡的最爽的了. 不废话,早上Matlab仿真干不出来,不如学学html,上代码~ <!DOCTYPE HTML><html>< ...

  3. vlan协议及端口类型

    一.VLAN协议 1.协议的应用 802.1Q协议,即Virtual Bridged Local Area Networks协议,主要规定了VLAN的实现. 2.协议结构 每一个支持802.1Q协议的 ...

  4. 第5章 绘图基础_5.1-5.4 GDI绘图

    5.1 GDI的原理和结构 (1)提供一种特殊机制彻底隔离应用程序与不同输出设备(eg.显示器或打印机),以便支持 与设备无关的图形. 光栅设备(如显示器.激光打印机):图像是由点构成的矩阵 图形输出 ...

  5. AC日记——过滤多余的空格 1.7 23

    23:过滤多余的空格 总时间限制:  1000ms 内存限制:   65536kB 描述 一个句子中也许有多个连续空格,过滤掉多余的空格,只留下一个空格. 输入 一行,一个字符串(长度不超过200), ...

  6. oracl中的集合操作符

    1:union(并集)     union连接两条sql语句,并且去除两条sql语句重复的记录 2.union all(并集) 接两句sql语句,两句sql语句的和不用去掉重复的记录. 3:inter ...

  7. iOS RunTime运行时(1):类与对象

    Objective-C语言是一门动态语言,他将很多静态语言在编译和链接期做的事放到了运行时来处理.这种动态语言的优势在于:我们写代码更具有灵活性,如我们可以把消息转发给我们想要的对象,或者随意交换一下 ...

  8. PHP 图片处理PNG颜色丢失

    根据需求做一个用户点击测试桃花运的小程序.在开发中需要使用PHP进行开发,原理是将用户的姓名通过php的图片处理写入图片中,此处遇到一巨坑. 就是png图片在调用 imagecolorallocate ...

  9. javascript中在链表中向前(向后)移动n个节点

     1.概念 在链表上移动n个节点,我第一眼看到这个需求的时候首先想到的是当前节点.使用这个当前节点作为参考来移动,没有这个当前节点的话是没有办法在链表上前进和后退的.初始化定义链表的时候定义一个当前节 ...

  10. Java 中包装类wrapped type之间以及和primitive type的比较

    注意, 包装类的实例之间比较, 是不能直接用 == 的 public static void main(String[] args) { // TODO Auto-generated method s ...