`
yidongkaifa
  • 浏览: 4064424 次
文章分类
社区版块
存档分类
最新评论

Android入门篇二:使用意图在Activity之间传递数据

 
阅读更多

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

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

Intent 负责对应用中一次操作的动作、动作涉及数据、附加数据进行描述,Android则根据此Intent的描述,负责找到对应的组件,将 Intent传递给调用的组件,并完成组件的调用。
因此,Intent在这里起着一个媒体中介的作用,专门提供组件互相调用的相关信息,实现调用者与被调用者之间的解耦。
通过Intent实现Activity之间数据传递步骤如下:
1、建立两个Activity, 分别作为发送端和接受端;

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

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


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

发送端MainActivity截图:


接收端OtherActivity截图:



以下是本项目的源代码:

一、MainActivity.java

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

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

<?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

<?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,大公告成!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics