1. 程式人生 > >Intent傳遞值,接收返回值

Intent傳遞值,接收返回值

第一個Activity程式清單

package com.demo.intenttest;

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;
import android.widget.Toast;


public class FirstActivity extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_first);


Button _btn = (Button) findViewById(R.id.bn);
_btn.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// 建立一個ComponentName 物件
// ComponentName comp = new
// ComponentName(FirstActivity.this,SecondActivity.class); //1
// Intent _intent = new Intent(); //2


// 為Intent設定Component屬性
// _intent.setComponent(comp); //3

Intent _intent = new Intent(FirstActivity.this,
SecondActivity.class); // 等價於1,2,3

//Intent傳值,Key必須唯一
_intent.putExtra("com.demo.intenttest.WULIKEY", "你好,中國");
// 普通啟動另一Activity
//startActivity(_intent);

//接收返回值
startActivityForResult(_intent, 1234);
}
});
}

//過載onActivityResult方法,接收返回的資訊
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == 4321) {
String valueResult= data.getExtras().getString("com.demo.intenttest.WULIKEY");
Toast.makeText(this, valueResult, Toast.LENGTH_SHORT).show();
}

super.onActivityResult(requestCode, resultCode, data);
}

}

第二個Activity程式清單

package com.demo.intenttest;


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;
import android.widget.Toast;


public class SecondActivity extends Activity {
Intent mintent;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.layout_second);


/*
* 接收FirstActivity 中1,2,3資訊 EditText show =(EditText)
* findViewById(R.id.show);

* ComponentName comp = getIntent().getComponent();

* show.setText("元件包名是:" + comp.getPackageName() + "\n元件類名是:" +
* comp.getClassName());
*/


mintent= getIntent();
String value;
if (mintent != null) {
// value接收Intent傳過來的值
value = this.getIntent().getExtras()
.getString("com.demo.intenttest.WULIKEY");
// 在螢幕上顯示接收到底值
Toast.makeText(this, value, Toast.LENGTH_SHORT).show();
}


Button btReturn = (Button) findViewById(R.id.buttonReturn);
btReturn.setOnClickListener(new OnClickListener() {


@Override
public void onClick(View v) {
// 修改從FirstActivity接收到的Intent傳過來的值
mintent.putExtra("com.demo.intenttest.WULIKEY", "世界,早安");
//將新值傳遞迴FirstActivity
setResult(4321, mintent);
finish(); // 關閉Activity


}
});
}
}

第一個Activity 佈局程式清單

<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="${relativePackage}.${activityClass}" >


    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/hello_world" />


    <Button
        android:id="@+id/bn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:text="Button" />


</RelativeLayout>

第二個Activity 佈局程式清單

<?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" >


    <EditText
        android:id="@+id/show"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="TextView" />


    <Button
        android:id="@+id/buttonReturn"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Return" />


</LinearLayout>

Manifest程式清單

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


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


    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name=".FirstActivity"
            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>
        <activity android:name=".SecondActivity" >
        </activity>
    </application>


</manifest>