天天看點

Android使用意圖傳遞資料的幾種方式

使用意圖傳遞資料的幾種方式

我們除了要從活動傳回資料,也常常要傳遞資料給活動。對此我們可以使用Intent對象将這些資料傳遞給目标活動。

1、建立一個名為PassingData的項目,在activity_main.xml檔案中添加一個Button:

<button android:id="@+id/btn_SecondActivity" 

android:layout_width="fill_parent" 

android:layout_height="wrap_content" 

android:οnclick="onClick" 

android:text="Click to go to Second Activity"></button>

2、在res/layout檔案夾中添加一個新的secondactivity.xml檔案,添加TextView和Button:

<TextView 

android:layout_width="fill_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:οnclick="onClick" 

android:text="Click to return to main activity">

</Button>

</TextView>

3、在目前包中建立一個Class,命名為SecondActivity,在SecondActivity.java中添加如下代碼:

package com.example.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) {

        // TODO Auto-generated method stub

        super.onCreate(savedInstanceState);

        setContentView(R.layout.secondactivity);

        // 為了獲得通過Intent對象發送的資料,先使用getIntent()方法擷取Intent對象,

        // 再調用該對象的getStringExtra()方法來獲得使用putExtra()方法設定的字元串值

        Toast.makeText(this, getIntent().getStringExtra("str1"),

                Toast.LENGTH_SHORT).show();

        // 同上,對于整數值,調用getIntExtra()方法(注意,如果該名稱沒有存儲值,則會使用預設值,在此為0)

        Toast.makeText(this,

                Integer.toString(getIntent().getIntExtra("age1", 0)),

                Toast.LENGTH_SHORT).show();

        // 擷取Bundle對象,要使用getExtras()方法:對于字元串值,使用getString();對于整數值,使用getInt()

        Bundle bundle = getIntent().getExtras();

        Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_SHORT)

                .show();

        Toast.makeText(this, Integer.toString(bundle.getInt("age2")),

                Toast.LENGTH_SHORT).show();

    }

    public void onClick(View v) {

        Intent intent = new Intent();

        intent.putExtra("age3", 45);

        // 回傳可以使用setData()方法(上一篇中講過)

        intent.setData(Uri.parse("Something passed back to main activity"));

        // 設定Intent和結果碼

        setResult(RESULT_OK, intent);

        // 關閉活動

        finish();

    }

}

4、在AndroidManifest.xml檔案中添加如下代碼:

    <intent-filter>

    <category android:name="android.intent.category.DEFAULT">

    </category></action></intent-filter>

</activity>

5、在MainActivity.java檔案中添加如下代碼:

public void onClick(View view) {

    Intent intent = new Intent("net.zenail.PassingData.SecondActivity");

    // 法一:使用putExtra()方法為Intent對象添加了兩個鍵/值對:String和integer類型

    intent.putExtra("str1", "This is a string");

    intent.putExtra("age1", 25);

    // 法二:建立Bundle對象,向其添加兩個鍵/值對,再使用putExtras添加給Intent對象

    Bundle extras = new Bundle();

    extras.putString("str2", "This is another string");

    extras.putInt("age2", 35);

    intent.putExtras(extras);

    // 啟動活動并設定1為請求碼

    startActivityForResult(intent, 1);

}

@Override

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

    // TODO Auto-generated method stub

    super.onActivityResult(requestCode, resultCode, data);

    if (requestCode == 1) {

        if (resultCode == RESULT_OK) {

            // 用getIntExtra()擷取用putExtra()設定的整數值

            Toast.makeText(this,

                    Integer.toString(data.getIntExtra("age3", 0)),

                    Toast.LENGTH_SHORT).show();

            // 用getData()擷取用setData()設定的字元串值

            Toast.makeText(this, data.getData().toString(),

                    Toast.LENGTH_SHORT).show();

        }

    }

}

6、運作,效果如下:

點選按鈕:

Android使用意圖傳遞資料的幾種方式

出現如下畫面:

Android使用意圖傳遞資料的幾種方式
Android使用意圖傳遞資料的幾種方式
Android使用意圖傳遞資料的幾種方式
Android使用意圖傳遞資料的幾種方式

消息框消失,點選按鈕,出現如下畫面:

Android使用意圖傳遞資料的幾種方式