Android新手入門教程(十一):使用Intents鏈接Activitiesの傳遞數據 – Android移動開發技術文章_手機開發 Android移動開發教學課程

除瞭能從一個Activity返回數據結果之外,向一個Activity傳遞數據也是很常用的。

    1.新建一個名為PassData的工程。

    2.main.xml中的代碼。

[java] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <Button 
        android:id="@+id/btn_SecondActivity" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:onClick="onClick" 
        android:text="Click to go to Second Activity" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <Button
        android:id="@+id/btn_SecondActivity"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="Click to go to Second Activity" />

</LinearLayout>    3.在res/layout文件夾下,創建secondactivity.xml文件。

[java] <?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:orientation="vertical" > 
 
    <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:onClick="onClick" 
        android:text="Click to return to main activity" /> 
 
</LinearLayout> 
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <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:onClick="onClick"
        android:text="Click to return to main activity" />

</LinearLayout>    4.新建一個Activity子類:SecondActivity.java。

[java] package net.horsttnann.PassingData; 
 
import net.horsttnann.PassingData.R; 
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 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.secondactivity); 
 
        // —get the data passed in using getStringExtra()—  
        Toast.makeText(this, getIntent().getStringExtra("str1"), 
                Toast.LENGTH_SHORT).show(); 
 
        // —get the data passed in using getIntExtra()—  
        Toast.makeText(this, 
                Integer.toString(getIntent().getIntExtra("age1", 0)), 
                Toast.LENGTH_SHORT).show(); 
 
        // —get the Bundle object passed in—  
        Bundle bundle = getIntent().getExtras(); 
 
        // —get the data using the getString()—  
        Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_SHORT) 
                .show(); 
 
        // —get the data using the getInt() method—  
        Toast.makeText(this, Integer.toString(bundle.getInt("age2")), 
                Toast.LENGTH_SHORT).show(); 
    } 
 
    public void onClick(View view) { 
        // —use an Intent object to return data—  
        Intent i = new Intent(); 
 
        // —use the putExtra() method to return some  
        // value—  
        i.putExtra("age3", 45); 
 
        // —use the setData() method to return some value—  
        i.setData(Uri.parse("Something passed back to main activity")); 
 
        // —set the result with OK and the Intent object—  
        setResult(RESULT_OK, i); 
 
        // —destroy the current activity—  
        finish(); 
    } 

package net.horsttnann.PassingData;

import net.horsttnann.PassingData.R;
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
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);

        // —get the data passed in using getStringExtra()—
        Toast.makeText(this, getIntent().getStringExtra("str1"),
                Toast.LENGTH_SHORT).show();

        // —get the data passed in using getIntExtra()—
        Toast.makeText(this,
                Integer.toString(getIntent().getIntExtra("age1", 0)),
                Toast.LENGTH_SHORT).show();

        // —get the Bundle object passed in—
        Bundle bundle = getIntent().getExtras();

        // —get the data using the getString()—
        Toast.makeText(this, bundle.getString("str2"), Toast.LENGTH_SHORT)
                .show();

        // —get the data using the getInt() method—
        Toast.makeText(this, Integer.toString(bundle.getInt("age2")),
                Toast.LENGTH_SHORT).show();
    }

    public void onClick(View view) {
        // —use an Intent object to return data—
        Intent i = new Intent();

        // —use the putExtra() method to return some
        // value—
        i.putExtra("age3", 45);

        // —use the setData() method to return some value—
        i.setData(Uri.parse("Something passed back to main activity"));

        // —set the result with OK and the Intent object—
        setResult(RESULT_OK, i);

        // —destroy the current activity—
        finish();
    }
}    5.AndroidManifest.xml中的代碼。

[java] <?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
    package="net.horsttnann.PassingData" 
    android:versionCode="1" 
    android:versionName="1.0" > 
 
    <uses-sdk android:minSdkVersion="10" /> 
 
    <application 
        android:icon="@drawable/ic_launcher" 
        android:label="@string/app_name" > 
        <activity 
            android:name=".PassingDataActivity" 
            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 
            android:name="net.horsttnann.PassingData.SecondActivity" 
            android:label="Second Activity" > 
            <intent-filter> 
                <action android:name="net.horsttnann.PassingDataSecondActivity" /> 
 
                <category android:name="android.intent.category.DEFAULT" /> 
            </intent-filter> 
        </activity> 
    </application> 
 
</manifest> 
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="net.horsttnann.PassingData"
    android:versionCode="1"
    android:versionName="1.0" >

    <uses-sdk android:minSdkVersion="10" />

    <application
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name" >
        <activity
            android:name=".PassingDataActivity"
            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
            android:name="net.horsttnann.PassingData.SecondActivity"
            android:label="Second Activity" >
            <intent-filter>
                <action android:name="net.horsttnann.PassingDataSecondActivity" />

                <category android:name="android.intent.category.DEFAULT" />
            </intent-filter>
        </activity>
    </application>

</manifest>    6.PassDataActivity中的代碼。

    7.按F11調試。

 

效果圖:

 程序第一次啟動:

    跳轉到SecondActivity:

    返回PassDataActivity:

  

摘自  horsttnann的專欄
 

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *