在Android应用中实现activity之间的跳转使用intent机制。
本例子简单地简绍如何利用intent使程序由MainActivity跳转到另一个OtherActivity实现单一参数值,在返回MainActivity时利用Bundle进行批量回传。
一、设计界面
1、MainActivity布局文件
打开res/layout/activity_main.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" >
- <Button
- android:id="@+id/open"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="打开另一个Activity(OtherActivity)" />
- <TextView
- android:id="@+id/result"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示OtherActivity返回结果" />
- </LinearLayout>
2、OtherActivity布局文件
打开res/layout/otheractivity.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" >
- <Button
- android:id="@+id/back"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="返回MainActivity" />
- <TextView
- android:id="@+id/prompt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="显示MainActivity传值" />
- </LinearLayout>
二、程序文件
1、打开“src/com.genwoxue.intent_b/MainActivity.java”文件。
然后输入以下代码:
- package com.genwoxue.intent_b;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class MainActivity extends Activity {
- private TextView tvResult=null;
- private Button btnOpen=null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- tvResult=(TextView)super.findViewById(R.id.result);
- btnOpen=(Button)super.findViewById(R.id.open);
- //调用OtherActivity,并且传值
- btnOpen.setOnClickListener(new OnClickListener(){
- public void onClick(View v)
- {
- Intent intent=new Intent(MainActivity.this,OtherActivity.class);
- intent.putExtra("prompt", "你知道蒋老夫子的电话与邮箱吗?");
- MainActivity.this.startActivityForResult(intent, 1);
- }
- });
- }
- //处理接收的数据
- @Override
- protected void onActivityResult(int requestCode,int resultCode,Intent data){
- switch(resultCode){
- case RESULT_OK:
- super.onActivityResult(requestCode, resultCode, data);
- //接收数据:采用Bundle传值
- Bundle bundle =data.getExtras();
- String employeeName=bundle.getString("employeeName");
- String mobile=bundle.getString("mobile");
- String email=bundle.getString("email");
- tvResult.setText("☆☆☆☆☆☆返回结果☆☆☆☆☆☆"+"\n员工:"+employeeName+"\n手机号码:"+mobile+"\n电子邮件:"+email);
- break;
- case RESULT_CANCELED:
- tvResult.setText("操作取消");
- break;
- default:
- break;
- }
- }
- }
2、打开“src/com.genwoxue.contentprovider_b/OtherActivity.java”文件。
然后输入以下代码:
- package com.genwoxue.intent_b;
- import android.os.Bundle;
- import android.app.Activity;
- import android.content.Intent;
- import android.view.View;
- import android.view.View.OnClickListener;
- import android.widget.Button;
- import android.widget.TextView;
- public class OtherActivity extends Activity {
- private TextView tvPrompt=null;
- private Button btnBack=null;
- @Override
- public void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_other);
- tvPrompt=(TextView)super.findViewById(R.id.prompt);
- btnBack=(Button)super.findViewById(R.id.back);
- //接收数据
- Intent intent=super.getIntent();
- String url=intent.getStringExtra("prompt");
- tvPrompt.setText(url);
- //返回MainActivity
- btnBack.setOnClickListener(new OnClickListener(){
- public void onClick(View v)
- {
- Bundle bundle = new Bundle();
- bundle.putString("employeeName", "蒋老夫子");
- bundle.putString("mobile", "139037100xx");
- bundle.putString("email", "hello@genwoxue.com");
- Intent intent = new Intent(OtherActivity.this,MainActivity.class);
- intent.putExtras(bundle);
- OtherActivity.this.setResult(RESULT_OK, intent);
- OtherActivity.this.finish();
- }
- });
- }
- }
三、配置文件
打开“AndroidManifest.xml”文件。
然后输入以下代码:
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.genwoxue.intent_b"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="8"
- android:targetSdkVersion="15" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name"
- android:theme="@style/AppTheme" >
- <activity
- android:name="com.genwoxue.intent_b.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>
- <span style="color:#ff0000;"><strong><activity
- android:name="com.genwoxue.intent_b.OtherActivity">
- </activity>
- </strong></span> </application>
- </manifest>
四、运行结果
来自:http://blog.youkuaiyun.com/jianghuiquan/article/details/8640922