首先,在这里稍微介绍一下意图(Intent)的概念:

Intent(意图)主要是解决Android应用的各项组件之间的通讯。

Intent 负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。

因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
通过Intent实现Activity之间数据传递步骤如下:

1、建立两个Activity, 分别作为发送端和接受端;

2、在发送端的Activity里面创建Intent对象,给Intent对象附加数据进去;

3、在接收端通过getIntent()获取传递过来的Intent对象,然后取出数据显示到TextView。

下面通过本人写的一个小例子代码来讲解,首先看一下运行的效果:

发送端MainActivity截图:

接收端OtherActivity截图:

以下是本项目的源代码:

一、MainActivity.java

[html]  view plain copy
  1. package com.intent.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.content.Intent;  
  5. import android.os.Bundle;  
  6. import android.view.View;  
  7. import android.view.View.OnClickListener;  
  8. import android.widget.Button;  
  9.   
  10. public class MainActivity extends Activity {  
  11.     private Button btn;  
  12.     @Override  
  13.     public void onCreate(Bundle savedInstanceState) {  
  14.         super.onCreate(savedInstanceState);  
  15.         setContentView(R.layout.main);  
  16.         btn = (Button)findViewById(R.id.btOpenOtherActivity);  
  17.         btn.setOnClickListener(new OnClickListener() {  
  18.             @Override  
  19.             public void onClick(View v) {  
  20.                 //定义一个意图  
  21.                 Intent intent = new Intent(MainActivity.this,OtherActivity.class);  
  22.                 //这里使用Bundle来传递数据  
  23.                 Bundle data = new Bundle();  
  24.                 data.putString("name", "wulianghuan");  
  25.                 data.putString("age", "22");  
  26.                 data.putString("address", "上海闵行");  
  27.                 intent.putExtras(data);  
  28.                 //启动意图  
  29.                 startActivity(intent);  
  30.             }  
  31.         });  
  32.     }  
  33. }  

二、OtherActivity.java

[html]  view plain copy
  1. package com.intent.activity;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.View;  
  6. import android.view.View.OnClickListener;  
  7. import android.widget.Button;  
  8. import android.widget.TextView;  
  9.   
  10. public class OtherActivity extends Activity {  
  11.     private TextView text_name;  
  12.     private TextView text_age;  
  13.     private TextView text_address;  
  14.   
  15.     @Override  
  16.     public void onCreate(Bundle savedInstanceState) {  
  17.         super.onCreate(savedInstanceState);  
  18.         setContentView(R.layout.other);  
  19.         text_name = (TextView) findViewById(R.id.name);  
  20.         text_age = (TextView) findViewById(R.id.age);  
  21.         text_address = (TextView) findViewById(R.id.address);  
  22.         //获取Intent传递的Bundle对象和它里面的数据  
  23.         Bundle data = getIntent().getExtras();  
  24.         String name = data.getString("name");  
  25.         String age = data.getString("age");  
  26.         String address = data.getString("address");  
  27.         //设置文本框的数据  
  28.         text_name.setText("姓名:"+name);  
  29.         text_age.setText("年龄:"+age);  
  30.         text_address.setText("地址:"+address);  
  31.     }  
  32. }  

三、布局文件main.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="这是:MainActivity" />  
  11.       
  12.     <Button   
  13.         android:layout_width="wrap_content"  
  14.         android:layout_height="wrap_content"  
  15.         android:id="@+id/btOpenOtherActivity"  
  16.         android:text="打开OtherActivity"/>  
  17.   
  18. </LinearLayout>  

四、布局文件other.xml

[html]  view plain copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent"  
  4.     android:layout_height="fill_parent"  
  5.     android:orientation="vertical" >  
  6.       
  7.     <TextView  
  8.         android:layout_width="fill_parent"  
  9.         android:layout_height="wrap_content"  
  10.         android:text="这里是OtherActivity"/>  
  11.     <TextView  
  12.         android:layout_width="fill_parent"  
  13.         android:layout_height="wrap_content"  
  14.         android:id="@+id/name"/>  
  15.     <TextView  
  16.         android:layout_width="fill_parent"  
  17.         android:layout_height="wrap_content"  
  18.         android:id="@+id/age"/>  
  19.     <TextView  
  20.         android:layout_width="fill_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:id="@+id/address"/>  
  23.       
  24. </LinearLayout>  

最后注意一点:一定不要忘了在AndroidManifest.xml 文件中对添加的Activity进行声明哦:

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

Ok,大公告成!