Activity传递参数——传递自定义数据类型
一.新建一个空的工程
二.在主界面中添加一个按钮
三.新建一个空的activity,并命名为TheAty
四.新建一个user类
//注意这里要实现Serializable,不然在传递参数时会出错
public class User 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 User(String name, int age)
{
this.name = name;
this.age = age;
}
}
五.修改MainActivity.java中的onCreate函数
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btnStartAty).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(MainActivity.this, TheAty.class);
i.putExtra("user", (Serializable) new User("Mary",20));//注意这里要做强制类型转换
startActivity(i);
}
});
六.在TheAty的布局文件中给textView加上id号
<TextView android:text="@string/hello_world" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/tv"/>
七.修改TheAty的源代码文件中的onCreate函数
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_the_aty);
Intent i = getIntent();
tv = (TextView)findViewById(R.id.tv);
User user = (User) i.getSerializableExtra("user");//注意这里要做强制类型转换
tv.setText(String.format("name=%s,age=%d",user.getName(),user.getAge()));
}
八.运行结果
Activity传递参数——传递自定义数据类型的更多相关文章
- Activity传递参数——传递复杂数据(Bunble包)
一.新建一个空的工程 二.在主界面中添加一个按钮 三.新建一个空的activity,并命名为TheAty 四.修改MainActivity.java中的onCreate函数 protected voi ...
- Activity传递参数——传递简单数据
一.新建一个空的工程 二.在主界面中添加一个按钮 三.新建一个空的activity,并命名为TheAty 四.修改MainActivity.java中的onCreate函数 protected voi ...
- javascript页面间传递参数
1.通过URL传递参数 传递参数页 function setCity() { var str = document.getElementById("cityName"); if ( ...
- 分发系统介绍 expect脚本远程登录 expect脚本远程执行命令 expect脚本传递参数
expect脚本远程登录 yum install -y expect yum install -y tcl tclx tcl-devel 自动远程登录 #! /usr/bin/expect set h ...
- Activity之间传递参数(三)
------siwuxie095 传递值对象,即自定义的有数据类型的对象 1.首先 new 一个 class:User,用于创建自定义对象,同时右键 Generate 出 Constructor.se ...
- Activity之间传递参数(四)
--------siwuxie095 获取Activity的返回参数 1.首先修改两个布局文件,都修改为 LinearLayout 布局, 添加orientation属性为:vertical. (1) ...
- Activity之间传递参数(二)
------siwuxie095 传递数据包 1.传递数据包要用到Bundle,MainActivity.java中: package com.siwuxie095.sendargs; import ...
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
1.smartimageview使用 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android&q ...
- 通过ModuleImplAdvertisement向自定义服务传递参数
无意中发现通过ModuleImplAdvertisement可以向自定义服务传递参数,有空试一试. —————————————————————————————————————————————————— ...
随机推荐
- Struts 2 类型转换器 输入校验 拦截器
Struts 2中内建了字符串类型和常见类型之间相互转换的转换器,能满足大多数转换需求,但不能完成字符串和User对象之间的转换. OGNL项目中有一个TypeConvert接口,这个接口是自定义类型 ...
- 001-前端系列-react系列
一.概述 原文地址:http://www.ruanyifeng.com/blog/2016/09/react-technology-stack.html 二.摘要 ES6 语法:教程 [可以了解] B ...
- 你真的会用Retrofit2吗?Retrofit2完全教程
本文注目录: Retrofit入门 Retrofit注解详解 Gson与Converter RxJava与CallAdapter 自定义Converter 自定义CallAdapter 其它说明 前言 ...
- 批量编译目录下文件的Makefile
1.多C文件生成各自可执行文件的Makefile如果一个目录下有很多C文件,且每个C文件都能生成一个独立的可执行文件,那么想全编译这些C文件并生成各作的可执行文件,在该目录下编写一个Makefile文 ...
- python提示AttributeError: 'NoneType' object has no attribute 'append'
在写python脚本时遇到AttributeError: 'NoneType' object has no attribute 'append' a=[] b=[1,2,3,4] a = a.appe ...
- 发布mvc3 遇到的HTTP错误 403.14-Forbidden Web 服务器被配置为不列出此目录的内容
今天在发布别人提供的MVC3的程序,正常部署后浏览报错,错误内容如图: 根据IIS提供的解决办法,启用目录浏览,刷新页面发现确实不报错了,但页面是以目录显示的,如图 虽然解决了报错问题,但不是正常的效 ...
- poj2092
/*水题,算每个号码处出现的次数*/ #include<stdio.h> #include<string.h> #include<algorithm> using ...
- [翻译]PostCSS简介
许多开发人员花时间在使用CSS的预处理器上如less,sass和stylus.这些工具已经成为Web开发的重要组成部分.写一个网站的样式,不使用嵌套,变量或混入等功能很少见.它们每个都是非常实用的,让 ...
- Spring_配置 Bean(1)
- java object 转为 json
JSONObject jsonObject=JSONObject.fromObject(map) 执行到这的时候没有任何反应的原因及解决办法 http://blog.csdn.net/tjcyjd/a ...