Android入门篇二:使用意图在Activity之间传递数据
2013-02-16 17:20
696 查看
首先,在这里稍微介绍一下意图(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 plaincopy
package com.intent.activity;
import android.app.Activity;
import android.content.Intent;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btOpenOtherActivity);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//定义一个意图
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
//这里使用Bundle来传递数据
Bundle data = new Bundle();
data.putString("name", "wulianghuan");
data.putString("age", "22");
data.putString("address", "上海闵行");
intent.putExtras(data);
//启动意图
startActivity(intent);
}
});
}
}
二、OtherActivity.java
[html] view plaincopy
package com.intent.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView text_name;
private TextView text_age;
private TextView text_address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
text_name = (TextView) findViewById(R.id.name);
text_age = (TextView) findViewById(R.id.age);
text_address = (TextView) findViewById(R.id.address);
//获取Intent传递的Bundle对象和它里面的数据
Bundle data = getIntent().getExtras();
String name = data.getString("name");
String age = data.getString("age");
String address = data.getString("address");
//设置文本框的数据
text_name.setText("姓名:"+name);
text_age.setText("年龄:"+age);
text_address.setText("地址:"+address);
}
}
三、布局文件main.xml
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是:MainActivity" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btOpenOtherActivity"
android:text="打开OtherActivity"/>
</LinearLayout>
四、布局文件other.xml
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这里是OtherActivity"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/age"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/address"/>
</LinearLayout>
最后注意一点:一定不要忘了在AndroidManifest.xml 文件中对添加的Activity进行声明哦:
<activity android:name=".OtherActivity"/>
Ok,大公告成!本文出自 “IT-人生精彩” 博客,请务必保留此出处http://mrwlh.blog.51cto.com/2238823/1134184
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 plaincopy
package com.intent.activity;
import android.app.Activity;
import android.content.Intent;
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;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
btn = (Button)findViewById(R.id.btOpenOtherActivity);
btn.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//定义一个意图
Intent intent = new Intent(MainActivity.this,OtherActivity.class);
//这里使用Bundle来传递数据
Bundle data = new Bundle();
data.putString("name", "wulianghuan");
data.putString("age", "22");
data.putString("address", "上海闵行");
intent.putExtras(data);
//启动意图
startActivity(intent);
}
});
}
}
二、OtherActivity.java
[html] view plaincopy
package com.intent.activity;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView text_name;
private TextView text_age;
private TextView text_address;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
text_name = (TextView) findViewById(R.id.name);
text_age = (TextView) findViewById(R.id.age);
text_address = (TextView) findViewById(R.id.address);
//获取Intent传递的Bundle对象和它里面的数据
Bundle data = getIntent().getExtras();
String name = data.getString("name");
String age = data.getString("age");
String address = data.getString("address");
//设置文本框的数据
text_name.setText("姓名:"+name);
text_age.setText("年龄:"+age);
text_address.setText("地址:"+address);
}
}
三、布局文件main.xml
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这是:MainActivity" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/btOpenOtherActivity"
android:text="打开OtherActivity"/>
</LinearLayout>
四、布局文件other.xml
[html] view plaincopy
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="这里是OtherActivity"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/name"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/age"/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/address"/>
</LinearLayout>
最后注意一点:一定不要忘了在AndroidManifest.xml 文件中对添加的Activity进行声明哦:
<activity android:name=".OtherActivity"/>
Ok,大公告成!本文出自 “IT-人生精彩” 博客,请务必保留此出处http://mrwlh.blog.51cto.com/2238823/1134184
相关文章推荐
- android 之 在Activity之间传递数据(使用意图传递数据之通用方式:)
- Android入门篇三:使用静态变量在Activity之间传递数据
- android之 在Activity之间使用意图传递数据(使用静态变量传递数据)
- Android入门篇五:使用全局变量在Activity之间传递数据
- Android入门篇四:使用全局变量在Activity之间传递数据
- Android入门篇三:使用静态变量在Activity之间传递数据
- Android入门篇四:使用剪切板在Activity之间传递对象数据
- 无废话Android之smartimageview使用、android多线程下载、显式意图激活另外一个activity,检查网络是否可用定位到网络的位置、隐式意图激活另外一个activity、隐式意图的配置,自定义隐式意图、在不同activity之间数据传递(5)
- 使用意图在Activity之间传递数据小插曲__传递自定义的序列化对象
- android 开发手记 activity 之间数据的传递 intent 对象的使用
- Android中在Activity之间传递数据的另一种方法, 使用application context
- Android Intent意图用于Activity之间的数据传递
- android 入门 Linearlayout 布局 初级 activity之间数据传递 xml内部定义监听
- android中使用Intent在activity之间传递数据
- 【Android中级教程(三)之不同Activity之间的数据传递---Bundle对象的使用】的改进!
- android开发入门-1(使用意图传递数据)
- android中使用Intent在activity之间传递数据
- android: activity之间传递复杂数据类型[使用Serializable序列化或者Parcelable序列化]
- Android开发从入门到放弃(1)在Activity之间传递数据
- Android开发之使用Intent在Activity之间传递数据