1、在實際應用中,我們不僅要向Activity中傳數據,也要從Activity中返回數據。雖然傳遞數據和返回數據類似,也可以采用前面四篇中提到的4種方法,但是一般建議采用Intent對象的方式來返回數據,使用這種方式返回數據,需要使用startActivityForResult方法來顯示Activity;
2、新建Android項目“android_intent_forresult”,打開布局文件“activity_main.xml”,添加“LinearLayout”、“TextView”、“EditView”等標簽,代碼如下:
~~~
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<EditText
android:id="@+id/one"
android:layout_width="20dp"
android:layout_height="wrap_content" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" + " >
</TextView>
<EditText
android:id="@+id/two"
android:layout_width="20dp"
android:layout_height="wrap_content" >
</EditText>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" = " >
</TextView>
<EditText
android:id="@+id/result"
android:layout_width="20dp"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="計算結果" >
</Button>
</LinearLayout>
~~~
效果:

3、新建布局文件“other.xml”,添加“TextView”、“EditView”、“Button”標簽,代碼如下:
~~~
<?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" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="@+id/msg"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</TextView>
<EditText
android:id="@+id/three"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</EditText>
</LinearLayout>
<Button
android:id="@+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="返回結果" >
</Button>
</LinearLayout>
~~~
4、新建“OtherActivity.java”文件,并使其繼承“Activity”,添加“onCreate”方法,代碼如下:
~~~
package com.android.myintent;
import android.app.Activity;
import android.os.Bundle;
public class OtherActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
}
}
~~~
5、在“AndroidManifest.xml”清單文件中加入“Activity”,加入代碼:
~~~
<activity android:name=".OtherActivity" >
</activity>
~~~
6、在“Main.java”中添加Button成員和“setOnClickListener”,實現兩個Button的跳轉,點擊第一個Activity后,出現第二個Activity;在此方法內部創建意圖,用“startActivityForResult”啟動意圖,并在Main類里重寫“onActivityResult”;添加“EditText”成員,實現數據的輸入并傳入Intent中。代碼如下:
~~~
package com.android.myintent;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
private Button button;
private final static int REQUESTCODE = 1;// 表示返回的結果碼
private EditText one, two, result; // 數據輸入
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText) this.findViewById(R.id.one);
two = (EditText) this.findViewById(R.id.two);
result = (EditText) this.findViewById(R.id.result);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// 點擊后獲得用戶錄入的值
int a = Integer.parseInt(one.getText().toString());
int b = Integer.parseInt(two.getText().toString());
// 創建意圖
Intent intent = new Intent(Main.this, OtherActivity.class);
// 將值傳入意圖
intent.putExtra("a", a);
intent.putExtra("b", b);
startActivityForResult(intent, REQUESTCODE);// 表示可以返回結果
}
});
}
// 再重寫一個onActivityResult方法,作用是將當前Activity中的數據傳遞到另一個Activity的意圖中后,實現跳轉,再回傳回來。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
~~~
7、在“OtherActivity.java”文件中添加Button和TextView成員,獲取意圖中的數據,代碼如下:
~~~
package com.android.myintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
public class OtherActivity extends Activity {
private Button button;
private TextView textView;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
// 實例化button和textview
button = (Button) this.findViewById(R.id.button2);
textView = (TextView) this.findViewById(R.id.msg);
Intent intent = getIntent(); // 獲取Intent
// 取出Intent中的值
int a = intent.getIntExtra("a", 0);
int b = intent.getIntExtra("b", 0);
textView.setText(a + " + " + b + " = " + " ? ");
}
}
~~~
運行一下,看下效果:

點擊“計算結果”,跳轉到第二個Activity:

8、回到“Main.java”文件中,從OtherActivity中獲取數據并顯示,代碼如下:
~~~
package com.android.myintent;
import android.R.integer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class Main extends Activity {
private Button button;
private final static int REQUESTCODE = 1;// 表示返回的結果碼
private EditText one, two, result; // 數據輸入
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
one = (EditText) this.findViewById(R.id.one);
two = (EditText) this.findViewById(R.id.two);
result = (EditText) this.findViewById(R.id.result);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
// 點擊后獲得用戶錄入的值
int a = Integer.parseInt(one.getText().toString());
int b = Integer.parseInt(two.getText().toString());
// 創建意圖
Intent intent = new Intent(Main.this, OtherActivity.class);
// 將值傳入意圖
intent.putExtra("a", a);
intent.putExtra("b", b);
startActivityForResult(intent, REQUESTCODE);// 表示可以返回結果
}
});
}
// 再重寫一個onActivityResult方法,作用是將當前Activity中的數據傳遞到另一個Activity的意圖中后,實現跳轉,再回傳回來。
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode == 2) {// 如果第二個Activity(OtherActivity)正常結束(“2”為返回碼resultCode)。
if (requestCode == REQUESTCODE) {// 如果返回狀態為1,即成功返回,就在意圖的返回值中取出數據。
int three = data.getIntExtra("three", 0);// 從第二個Activity中返回意圖中的數據。
result.setText(String.valueOf(three));
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
~~~
9、在“OtherActivity.java”文件中,添加點擊Button事件,使數據回傳~,代碼如下:
~~~
package com.android.myintent;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
public class OtherActivity extends Activity {
private Button button;
private TextView textView;
private EditText editText;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);
// 實例化button和textview
button = (Button) this.findViewById(R.id.button2);
textView = (TextView) this.findViewById(R.id.msg);
editText = (EditText) this.findViewById(R.id.three);
Intent intent = getIntent(); // 獲取Intent
// 取出Intent中的值
int a = intent.getIntExtra("a", 0);
int b = intent.getIntExtra("b", 0);
textView.setText(a + " + " + b + " = " + " ? ");
// 添加點擊事件并回傳數據
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent = new Intent();// 重新聲明一個意圖。
int three = Integer.parseInt(editText.getText().toString());// 獲取輸入的值。
intent.putExtra("three", three); // 將three回傳到意圖中。
// 通過Intent對象返回結果,調用setResult方法。
setResult(2, intent);// resultCode為大于1的數,隨意選取,為2即可。
finish();// 結束當前Activity的生命周期。
}
});
}
}
~~~
10、運行,結果:
(1)輸入2和3:

(2)單擊“計算結果”,跳轉:

(3)輸入5,單擊“返回結果”,數據回傳:

實現要點:
(1)在“Main.java”中,創建Intent并啟動Activity,調用“startActivityForResult”,并定義當前請求碼;
(2)重寫“onActivityResult”方法,并設置條件,若滿足返回碼值,則將第二個Activity中的數據傳回來,賦給當前Activity的“result”編輯框;
(3)在“OtherActivity.java”中,再創建一個意圖,將數據填寫到意圖中,通過意圖將結果回傳(通過“setResult”方法);
(4)結束當前Activity生命周期;
- 前言
- Android應用程序剖析
- (一)——生命周期
- (二)——使用Intent傳數據之通用方式
- (三)——使用靜態變量傳遞數據
- (四)——通過剪切板傳遞數據
- (五)——通過全局變量傳遞數據
- (六)——從Activity返回數據
- adt-bundle-linux-x86_64-20131030下新建工程提示找不到adb和R.java問題的解決
- Eclipse啟動時提示fail to create the Java Virtual Machine問題的解決
- Android常見UI組件之ListView(一)
- Android常見UI組件之ListView(二)——定制ListView
- (七)——顯示對話框窗口
- (八)——顯示進度對話框
- (九)——更復雜的進度對話框
- (十)——使用意圖鏈接活動
- (十一)——從意圖返回結果
- (十二)——使用意圖傳遞數據的幾種方式
- (十三)——碎片(一)
- (十四)——在運行時添加碎片(附源碼)
- (十五)——碎片的生命周期(附源碼)
- (十六)——碎片之間進行交互(附源碼)
- (十七)——使用意圖調用內置應用程序
- (十八)——使用意圖篩選器和實現瀏覽網頁(附源碼)