利用Intent进行Activity之间的通信(1)

Intent对象的基本概念:它是Android应用程序组件之一,表示一种意图。Intent当中最重要的内容是action与data.


使用Intent对象传递数据的基本步骤为:

1、在需要传递数据的Activity中使用putExtra()系列方法向Intent对象当中存储数据;

2、在接受数据的Activity中使用getXXXExtra()系列方法从Intent对象当中取出数据。


下面用例子说明。



在第一个Activity中有一个按钮,点击后启动OtherActivity,并传递Intent对象,再把当中数据显示出来。

MainActivity.java

package com.wyb.s02_e03_intent;

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 button;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		button =(Button)findViewById(R.id.button);
		button.setOnClickListener(new ButtonListener());
	}
	class ButtonListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			Intent intent=new Intent();
			intent.setClass(MainActivity.this, OtherActivity.class);
			intent.putExtra("com.wyb.s02_e03_intent.Age", 20);//注意用双引号把报名括起来
			intent.putExtra("com.wyb.s02_e03_intent.Name", "ZhangSan");
			startActivity(intent);
		}
		
	}
}

OtherActivity.java

package com.wyb.s02_e03_intent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.TextView;

public class OtherActivity extends Activity {

	private TextView textView;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.other);
		
		textView = (TextView)findViewById(R.id.textView);
		Intent intent = getIntent();
		
		int age = intent.getIntExtra("com.wyb.s02_e03_intent.Age", 10);
		String name = intent.getStringExtra("com.wyb.s02_e03_intent.Name");
		textView.setText("姓名:"+name+" 年龄:"+age+" ");
		
		
	}

}

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <Button
        android:id="@+id/button"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="启动第二个Activity" />

</RelativeLayout>

other.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <TextView 
        android:id="@+id/textView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="other"/>

</LinearLayout>

AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.wyb.s02_e03_intent"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk
        android:minSdkVersion="17"
        android:targetSdkVersion="17" />

    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.wyb.s02_e03_intent.MainActivity"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity 
            android:name="com.wyb.s02_e03_intent.OtherActivity"
            android:label="otherActivity"></activity>
    </application>

</manifest>



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值