使用意图传递数据之通用方式。

测试应用:当前页面点击button传递数据到一个新的页面显示在textview中。

首先在,mainActivity.xml文件中加入一个button按钮

<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="测试使用Intent传递数据" >
</Button>

然后在,MainActivity.java中加入

private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);//加载布局文件
button=(Button)this.findViewById(R.id.button);//获取button
button.setOnClickListener(new View.OnClickListener() {//点击button事件
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//创建意图
Intent intent=new Intent(Main.this, OtherActivity.class);
//在意图中传递数据
intent.putExtra("name", "张三");
intent.putExtra("age", );
intent.putExtra("address", "北京");
//启动意图
startActivity(intent);
}
});
// Log.i(TAG, "onCreate methos is executed");
}

在布局文件中,新建一个other.xml文件。

<TextView
android:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TextView>

然后新建一个OtherActivity.class 并加入

private TextView textView;

    @Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub super.onCreate(savedInstanceState);
setContentView(R.layout.other);
textView = (TextView)this.findViewById(R.id.msg);//获取文本框
Intent intent = getIntent();//获取意图对象
Integer age = intent.getIntExtra("age", );//获取意图对象数据
String name = intent.getStringExtra("name"); //获取意图对象数据
String address = intent.getStringExtra("address");//获取意图对象数据
textView.setText("age--->>"+age+"\n"+"name--->>"+name+"\n"+"address--->>"+address);//设置到文本框
}

最后不要忘记在清单文件中加入OtherActivity。

<activity android:name=".OtherActivity"></activity>

Android 使用意图传递数据的更多相关文章

  1. Android学习笔记(十二)——使用意图传递数据的几种方式

    使用意图传递数据的几种方式 点此获取完整代码 我们除了要从活动返回数据,也经常要传递数据给活动.对此我们能够使用Intent对象将这些数据传递给目标活动. 1.创建一个名为PassingData的项目 ...

  2. Android-Intent意图传递数据

    Intent意图传递基本数据类型: OuterActivity 激活启动 OneActivity 用Intent携带基本数据类型: /** * Intent意图传递数据到另外一个Activity */ ...

  3. Android剪切板传递数据传递序列化对象数据

    一.剪切板的使用介绍 1. 剪切板对象的创建 使用剪切板会用到,ClipboardManager对象,这个对像的创建不可以使用构造方法,主要是由于没有提供public的构造函数(单例模式),需要使用A ...

  4. Android 使用全局变量传递数据

    使用全局变量传递数据,所谓的全局变量类似于jee开发中的application变量.申明后,全局调用.只有当内存被清理后,才被销毁.否则一直可以调用. 还是使用点击一个button,传递一个数据到另一 ...

  5. Android与NativeC传递数据不正确问题

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Android studio 2.3.3 这两天一直在调试一个BUG,具体为通过 NativeC 来处理上层Android ...

  6. android使用全局变量传递数据

    android中Application是用来保存全局变量的,在package创建的时候就存在了,到所有的activity都被destroy掉之后才会被释放掉.所以当我们需要全局变量的时候只要在appl ...

  7. Android 通过Application 传递数据

    </pre><pre> package com.example.ApplicationTest; import android.app.Application; /** * C ...

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

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

  9. Android-Application来传递数据

    在上一篇博客中介绍到,Android-Intent意图传递数据,能够传递基本数据类型系列,能够传递对象(需要序列化),等操作: 但是如果要传递 List<T>,这种类型的数据,就不能通过I ...

随机推荐

  1. Android -- 经验分享

    目录                                                                                             代码中安装 ...

  2. pietty and putty safe password

    如何让putty记住密码..pietty也一样的不能记住密码. 找不到好的的方法...只好试着按照参数格式做了一个快捷方式..F:\soft\pietty.exe -pw password123 ro ...

  3. nodejs笔记二--文件I/O;

    一.写入文件: fs.writeFile(filename, data, callback),数据参数可以是string或者是Buffer,编码格式参数可选,默认为"utf8",回 ...

  4. UITableView的常用属性和cell的内存优化

    UITableView的常用属性: 分割线颜色设置: 1> 设置separatorStyle: 分割线的颜色 方法:tableView.separatorStyle = UITableViewC ...

  5. 从C中变化过来的各种语言的printf输出格式

    在c.php和shell中经常可以碰到printf的使用,特别是在php中printf的相关变种有好几个:print.printf.sprintf.vprintf.vsprintf     在这些语言 ...

  6. Careercup - Microsoft面试题 - 5752271719628800

    2014-05-10 20:31 题目链接 原题: Given an array of integers and a length L, find a sub-array of length L su ...

  7. 21、HierarchyViewer使用记录

    1.是啥 HierachyViewer是一种能够方便开发人员了解activity中的布局信息的工具. 2.异常 HierachyViewer在未root过的设备或者低版本的设备是无法使用的 3.怎么办 ...

  8. 【Count and Say】cpp

    题目: The count-and-say sequence is the sequence of integers beginning as follows:1, 11, 21, 1211, 111 ...

  9. POSIX 线程详解 一种支持内存共享的简捷工具

    线程是有趣的 了解如何正确运用线程是每一个优秀程序员必备的素质.线程类似于进程.如同进程,线程由内核按时间分片进行管理.在单处理器系统中,内核使用时间分片来模拟线程的并发执行,这种方式和进程的相同.而 ...

  10. BZOJ3130 [Sdoi2013]费用流

    AC通道:http://www.lydsy.com/JudgeOnline/problem.php?id=3130 这题codevs上也有,不过数据挂了[要A得看discuss]. 题目大意: Ali ...