1. 程式人生 > >2.3 使用intent物件傳遞資料

2.3 使用intent物件傳遞資料

除了從intent返回資料之外,也經長需要傳遞資料給activity。
建立一個android專案:PassingData
在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
=".MainActivity" >
<Button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="wrap_content" android:text="Click to go to Second Activity " android:onClick="onClick" /> </RelativeLayout>

在res/layout中,新建xml檔案命名為:secondactivity.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:layout_width="match_parent"
android:layout_height="wrap_content" android:text="Welcome to second activity" />
<Button android:id="@+id/btn_MainActivity" android:layout_width="fill_parent" android:layout_height="wrap_content" android:text="Click to return to main activity" android:onClick="onClick"/> </LinearLayout>

在包中新增一個新的class檔案。命名為SecondActivity:

package net.learn2develop.passingdata;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class SecondActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
        //使用getIntent()方法獲取該Intent物件,再呼叫getStringExtra()獲取使用putExtra()方法設定的字串值
        Toast.makeText(this, getIntent().getStringExtra("str1"), Toast.LENGTH_SHORT).show();
        //使用getIntExtra()獲取int值
        Toast.makeText(this, Integer.toString(getIntent().getIntExtra("age1", 0)), Toast.LENGTH_SHORT).show();
        //為了回去Bundle物件需要使用getExtras()方法
        Bundle bundle=getIntent().getExtras();
        //使用getString()獲取字串值
        Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_SHORT).show();
        //使用getInt()方法獲取整數值
        Toast.makeText(this, Integer.toString(bundle.getInt("age2")), Toast.LENGTH_SHORT).show();
    }
    public void onClick(View v){
        //使用intent物件返回資料
        Intent i=new Intent();
        //使用putExtra()方法返回某個值
        i.putExtra("age3", "45");
        //為了使被呼叫活動可以返回一個值給呼叫它的activity可以通過setData()方法使用一個Intent物件來傳遞資料
        i.setData(Uri.parse("Something passed back to main activity"));
        //setResult()方法設定了一個結果碼(RESULT_OK或RESULT_CANCELLED)和回傳給呼叫活動的資料(一個intent物件)
        setResult(RESULT_OK, i);
        //銷燬當前activity
        finish();
    }
}

在MainActivity中:

package net.learn2develop.passingdata;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Toast;

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
    public void onClick(View v){
        /**activity中傳遞資料的方法:使用Intent物件的putExtra()方法新增一個鍵值對
         * 還可以建立一個bundle物件,並使用putExtras()方法將Bundle物件新增給Intent物件*/
        Intent i=new Intent("net.learn2develop.passingdata.SecindActivity");
        //使用purExtra()方法新增新的 name/value
        i.putExtra("str1", "this is a String");
        i.putExtra("age1", 25);
        //使用Bundle物件新增name/value
        Bundle extras=new Bundle();
        extras.putString("str2", "This is another string");
        extras.putInt("age2", 35);
        //把Bundle物件新增到intent物件
        i.putExtras(extras);
        //啟動activity獲得返回結果
        startActivityForResult(i, 1);
    }
    //在呼叫者activity中,需要實現onActivityResult()方法,一個activity無論何時返回都要呼叫這份個方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if(requestCode==1){
            if (resultCode==RESULT_OK) {
                Toast.makeText(this, Integer.toString(data.getIntExtra("age3", 0)), Toast.LENGTH_SHORT).show();
                Toast.makeText(this, data.getData().toString(), Toast.LENGTH_SHORT).show();
            }
        }
    }
}