Android——startActivityForResult的詳細用法

來源:互聯網
上載者:User
在啟動另外一個Activity的時候,有兩種方法,一種是直接使用startActivity,另外一種就是使用startActivityForResult。前一種想必大家都明白怎麼使用了,我就不廢話了。本文主要通過一個Demo來學習一下第二種。

startActivityForResult的主要作用就是它可以回傳資料,假設我們有兩個頁面,首先進入第一個頁面,裡面有一個按鈕,用於進入下一個頁面,當進入下一個頁面時,進行設定作業,並在其finish()動作或者back動作後,將設定的值回傳給第一個頁面,從而第一個頁面來顯示所得到的值。這個有一點像回調方法,就是在第二個頁面finish()動作或者back動作後,會回調第一個頁面的onActivityResult()方法,所以我們可以重寫一下這個方法。直接看代碼吧:
第一個頁面代碼:

package org.sunchao;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.TextView;public class TestStartActivityForResultActivity extends Activity implementsOnClickListener {private TextView mText01;private TextView mText02;private Button button01;private Button button02;private Intent mIntent;private int requestCode;/** Called when the activity is first created. */@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);mText01 = (TextView) findViewById(R.id.mText01);mText02 = (TextView) findViewById(R.id.mText02);button01 = (Button) findViewById(R.id.mButton01);button02 = (Button) findViewById(R.id.mButton02);button01.setOnClickListener(this);button02.setOnClickListener(this);mText01.setText("01");mText02.setText("02");mIntent = new Intent();mIntent.setClass(TestStartActivityForResultActivity.this,Activity02.class);}@Overridepublic void onClick(View v) {switch (v.getId()) {case R.id.mButton01:// 請求碼的值隨便設定,但必須>=0requestCode = 0;startActivityForResult(mIntent, requestCode);break;case R.id.mButton02:requestCode = 2;startActivityForResult(mIntent, requestCode);break;default:break;}}// 回調方法,從第二個頁面回來的時候會執行這個方法@Overrideprotected void onActivityResult(int requestCode, int resultCode, Intent data) {String change01 = data.getStringExtra("change01");String change02 = data.getStringExtra("change02");// 根據上面發送過去的請求嗎來區別switch (requestCode) {case 0:mText01.setText(change01);break;case 2:mText02.setText(change02);break;default:break;}}}

第一個頁面配置檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:id="@+id/mText01"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     /><TextView      android:id="@+id/mText02"    android:layout_width="fill_parent"     android:layout_height="wrap_content"     /><Button     android:id="@+id/mButton01"    android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:text="改變第一行文本的值"/><Button     android:id="@+id/mButton02"    android:layout_width="fill_parent"     android:layout_height="wrap_content"    android:text="改變第二行文本的值"/></LinearLayout>

第二個頁面代碼:

package org.sunchao;import android.app.Activity;import android.content.Intent;import android.os.Bundle;public class Activity02 extends Activity {private int resultCode = 0;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity02);Intent mIntent = new Intent();mIntent.putExtra("change01", "1000");mIntent.putExtra("change02", "2000");// 設定結果,並進行傳送this.setResult(resultCode, mIntent);// this.finish();}}

第二個頁面配置檔案:

<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"    android:orientation="vertical"    android:layout_width="fill_parent"    android:layout_height="fill_parent"    ><TextView      android:layout_width="fill_parent"     android:layout_height="wrap_content"     android:text="文本的值已經改變"    /></LinearLayout>

AndroidManifest.xml:

<?xml version="1.0" encoding="utf-8"?><manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="org.sunchao"      android:versionCode="1"      android:versionName="1.0">    <uses-sdk android:minSdkVersion="8" />    <application android:icon="@drawable/icon" android:label="@string/app_name">        <activity android:name=".TestStartActivityForResultActivity"                  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=".Activity02" />    </application></manifest>

運行:

代碼:http://download.csdn.net/source/3448804

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.