在Android应用中实现activity之间的跳转使用intent机制。
本例子简单地简绍如何利用intent使程序由MainActivity跳转到另一个OtherActivity实现单一参数值,在返回MainActivity时利用Bundle进行批量回传。
一、设计界面
1、MainActivity布局文件
打开res/layout/activity_main.xml文件。
输入以下代码:
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout
3. xmlns:android="http://schemas.android.com/apk/res/android"
4. android:layout_width="match_parent"
5. android:layout_height="match_parent"
6. android:orientation="vertical" >
7.
8. <Button
9. android:id="@+id/open"
10. android:layout_width="wrap_content"
11. android:layout_height="wrap_content"
12. android:text="打开另一个Activity(OtherActivity)" />
13.
14. <TextView
15. android:id="@+id/result"
16. android:layout_width="wrap_content"
17. android:layout_height="wrap_content"
18. android:text="显示OtherActivity返回结果" />
19.
20. </LinearLayout>
2、OtherActivity布局文件
打开res/layout/otheractivity.xml文件。
输入以下代码:
1. <?xml version="1.0" encoding="utf-8"?>
2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3. android:layout_width="match_parent"
4. android:layout_height="match_parent"
5. android:orientation="vertical" >
6.
7. <Button
8. android:id="@+id/back"
9. android:layout_width="wrap_content"
10. android:layout_height="wrap_content"
11. android:text="返回MainActivity" />
12.
13. <TextView
14. android:id="@+id/prompt"
15. android:layout_width="wrap_content"
16. android:layout_height="wrap_content"
17. android:text="显示MainActivity传值" />
18.
19. </LinearLayout>
二、程序文件
1、打开“src/com.genwoxue.intent_b/MainActivity.java”文件。
然后输入以下代码:
1. package com.genwoxue.intent_b;
2.
3. import android.os.Bundle;
4. import android.app.Activity;
5. import android.content.Intent;
6. import android.view.View;
7. import android.view.View.OnClickListener;
8. import android.widget.Button;
9. import android.widget.TextView;
10.
11. public class MainActivity extends Activity {
12.
13. private TextView tvResult=null;
14. private Button btnOpen=null;
15. @Override
16. public void onCreate(Bundle savedInstanceState)
17. {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.activity_main);
20.
21. super.findViewById(R.id.result);
22.
23. super.findViewById(R.id.open);
24.
25. //调用OtherActivity,并且传值
26. new OnClickListener(){
27. public void onClick(View v)
28. {
29. new Intent(MainActivity.this,OtherActivity.class);
30. "prompt", "你知道蒋老夫子的电话与邮箱吗?");
31. this.startActivityForResult(intent, 1);
32. }
33. });
34. }
35.
36. //处理接收的数据
37. @Override
38. protected void onActivityResult(int requestCode,int resultCode,Intent data){
39. switch(resultCode){
40. case RESULT_OK:
41. super.onActivityResult(requestCode, resultCode, data);
42. //接收数据:采用Bundle传值
43. Bundle bundle =data.getExtras();
44. "employeeName");
45. "mobile");
46. "email");
47. "☆☆☆☆☆☆返回结果☆☆☆☆☆☆"+"\n员工:"+employeeName+"\n手机号码:"+mobile+"\n电子邮件:"+email);
48. break;
49. case RESULT_CANCELED:
50. "操作取消");
51. break;
52. default:
53. break;
54. }
55. }
56. }
57.
2、打开“src/com.genwoxue.contentprovider_b/OtherActivity.java”文件。
然后输入以下代码:
1. package com.genwoxue.intent_b;
2.
3. import android.os.Bundle;
4. import android.app.Activity;
5. import android.content.Intent;
6. import android.view.View;
7. import android.view.View.OnClickListener;
8. import android.widget.Button;
9. import android.widget.TextView;
10.
11. public class OtherActivity extends Activity {
12.
13. private TextView tvPrompt=null;
14. private Button btnBack=null;
15. @Override
16. public void onCreate(Bundle savedInstanceState)
17. {
18. super.onCreate(savedInstanceState);
19. setContentView(R.layout.activity_other);
20. super.findViewById(R.id.prompt);
21. super.findViewById(R.id.back);
22.
23. //接收数据
24. super.getIntent();
25. "prompt");
26. tvPrompt.setText(url);
27.
28. //返回MainActivity
29. new OnClickListener(){
30. public void onClick(View v)
31. {
32. new Bundle();
33. "employeeName", "蒋老夫子");
34. "mobile", "139037100xx");
35. "email", "hello@genwoxue.com");
36.
37. new Intent(OtherActivity.this,MainActivity.class);
38. intent.putExtras(bundle);
39. this.setResult(RESULT_OK, intent);
40. this.finish();
41. }
42. });
43. }
44. }
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
1. <?xml version="1.0" encoding="utf-8"?>
2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
3. package="com.genwoxue.intent_b"
4. android:versionCode="1"
5. android:versionName="1.0" >
6.
7. <uses-sdk
8. android:minSdkVersion="8"
9. android:targetSdkVersion="15" />
10.
11. <application
12. android:allowBackup="true"
13. android:icon="@drawable/ic_launcher"
14. android:label="@string/app_name"
15. android:theme="@style/AppTheme" >
16. <activity
17. android:name="com.genwoxue.intent_b.MainActivity"
18. android:label="@string/app_name" >
19. <intent-filter>
20. <action android:name="android.intent.action.MAIN" />
21. <category android:name="android.intent.category.LAUNCHER" />
22. </intent-filter>
23. </activity>
24.
25. <span style="color:#ff0000;"><strong><activity
26. android:name="com.genwoxue.intent_b.OtherActivity">
27. </activity>
28. </strong></span> </application>
29.
30. </manifest>
四、运行结果