Android新手入門教程(十):使用Intents連結Activitiesの返回結果

來源:互聯網
上載者:User

startActivity()方法可以調用另外的Activity,但這種方法不會給當前的Activity返回一個結果。例如,你有一個Activity提示使用者輸入使用者名稱和密碼,使用者輸入的資訊需要被“回傳”給這個輸入資訊的Activity,那就需要使用startActivityForResult()方法。

    1.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="This is the Second Activity!" /> 
 
    <TextView 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:text="Please enter your name" /> 
 
    <EditText 
        android:id="@+id/txt_username" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" /> 
 
    <Button 
        android:id="@+id/btn_OK" 
        android:layout_width="fill_parent" 
        android:layout_height="wrap_content" 
        android:onClick="onClick" 
        android:text="OK" /> 
 
</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="This is the Second Activity!" />

    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Please enter your name" />

    <EditText
        android:id="@+id/txt_username"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content" />

    <Button
        android:id="@+id/btn_OK"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:onClick="onClick"
        android:text="OK" />

</LinearLayout>    2.SecondActivity.java中的代碼。


[java] package net.horsttnann.UsingIntent; 
 
import android.app.Activity; 
import android.content.Intent; 
import android.net.Uri; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.EditText; 
 
public class SecondActivity extends Activity { 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.secondactivity); 
    } 
 
    public void onClick(View view) { 
        Intent data = new Intent(); 
 
        // ---get the EditText view---  
        EditText txt_username = (EditText) findViewById(R.id.txt_username); 
 
        // ---set the data to pass back---  
        data.setData(Uri.parse(txt_username.getText().toString())); 
        setResult(RESULT_OK, data); 
 
        // ---closes the activity---  
        finish(); 
    } 

package net.horsttnann.UsingIntent;

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

public class SecondActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.secondactivity);
    }

    public void onClick(View view) {
        Intent data = new Intent();

        // ---get the EditText view---
        EditText txt_username = (EditText) findViewById(R.id.txt_username);

        // ---set the data to pass back---
        data.setData(Uri.parse(txt_username.getText().toString()));
        setResult(RESULT_OK, data);

        // ---closes the activity---
        finish();
    }
}
    3.UsingIntentActivity.java中的代碼。


[java] package net.horsttnann.UsingIntent; 
 
 
import android.app.Activity; 
import android.content.Intent; 
import android.os.Bundle; 
import android.view.View; 
import android.widget.Toast; 
 
 
public class UsingIntentActivity extends Activity { 
    int request_Code = 1; 
 
 
    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
        super.onCreate(savedInstanceState); 
        setContentView(R.layout.main); 
    } 
 
 
    public void onClick(View view) { 
        // startActivity(new Intent("net.horsttnann.SecondActivity"));  
        // or  
        // startActivity(new Intent(this, SecondActivity.class));  
 
 
        startActivityForResult(new Intent("net.horsttnann.SecondActivity"), 
                request_Code); 
    } 
 
 
    public void onActivityResult(int requestCode, int resultCode, Intent data) { 
        if (requestCode == request_Code) { 
            if (resultCode == RESULT_OK) { 
                Toast.makeText(this, data.getData().toString(), 
                        Toast.LENGTH_SHORT).show(); 
            } 
        } 
    } 

package net.horsttnann.UsingIntent;


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


public class UsingIntentActivity extends Activity {
    int request_Code = 1;


    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }


    public void onClick(View view) {
        // startActivity(new Intent("net.horsttnann.SecondActivity"));
        // or
        // startActivity(new Intent(this, SecondActivity.class));


        startActivityForResult(new Intent("net.horsttnann.SecondActivity"),
                request_Code);
    }


    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == request_Code) {
            if (resultCode == RESULT_OK) {
                Toast.makeText(this, data.getData().toString(),
                        Toast.LENGTH_SHORT).show();
            }
        }
    }
}    4.按F11調試。

 


    程式第一次被載入:

 
    點擊按鈕,使用startActivityForResult()方法調用SecondActivity:

    點擊OK按鈕,返回到UsingIntentActivity:

  

摘自  horsttnann的專欄
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.