您的位置:首页 > 其它

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

2014-05-07 09:48 405 查看
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>
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: