在send_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:orientation="vertical"
android:gravity="center_horizontal">
<Button
android:id="@+id/mybut"
android:layout_marginTop="8dp"
android:layout_width="180dp"
android:layout_height="40dp"
android:background="#3399ff"
android:textColor="#ffffff"
android:text="发送Intent的Activity程序"/>
<TextView
android:id="@+id/text"
android:layout_marginTop="18dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="25dp"
android:textColor="#ffffff"/>
</LinearLayout>
在receive_main.xml中:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:gravity="center_horizontal"
android:background="#000000">
<TextView
android:id="@+id/show"
android:layout_marginTop="8dp"
android:gravity="center_horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textSize="30dp"
android:textColor="#ffffff"/>
<Button
android:id="@+id/retbut"
android:layout_marginTop="18dp"
android:layout_width="60dp"
android:layout_height="40dp"
android:textColor="#ffffff"
android:background="#3399ff"
android:text="返回数据"/>
</LinearLayout>
在Send.java中:
package com.li.intentproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class Send extends Activity {
private Button mybut = null;
private TextView text = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.send_main);
this.mybut = (Button)super.findViewById(R.id.mybut);
this.text = (TextView)super.findViewById(R.id.text);
this.mybut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener{
public void onClick(View v) {
Intent it = new Intent(Send.this,Receive.class); //要跳转的名称
it.putExtra("myinfo","骐骥一跃,不能十步;"+"\n"+"驽马十驾,功在不舍。"); //要穿的值
Send.this.startActivityForResult(it, 1); //数据大于1就可以接收
}
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch(resultCode) {
case RESULT_OK:
Send.this.text.setText(data.getStringExtra("ret")) ;
break ;
case RESULT_CANCELED:
Send.this.text.setText("操作取消。") ;
break ;
default:
break;
}
}
}
在Receive.java中:
package com.li.intentproject;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.support.v4.app.NavUtils;
public class Receive extends Activity {
private TextView show = null;
private Button retbut = null;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
super.setContentView(R.layout.receive_main);
this.show = (TextView)super.findViewById(R.id.show);
this.retbut = (Button)super.findViewById(R.id.retbut);
Intent it = super.getIntent(); //取得当前的Intent
String info = it.getStringExtra("myinfo"); //取得数据
this.show.setText(info);
this.retbut.setOnClickListener(new OnClickListenerImpl());
}
private class OnClickListenerImpl implements OnClickListener {
public void onClick(View v) {
Receive.this.getIntent().putExtra("ret", "不登高山,不知天之高也;" + "\n" + "不临深溪,不知地之厚也。") ;
Receive.this.setResult(RESULT_OK, Receive.this.getIntent()) ;
Receive.this.finish() ;
}
}
}
在AndroidManifest.xml中:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.li.intentproject"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="15" />
<application
android:icon="@drawable/ic_launcher"
android:label="@string/app_name"
android:theme="@style/AppTheme" >
<activity
android:name=".Send"
android:label="@string/title_activity_send" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="Receive">
</activity>
</application>
</manifest>