**使用意圖傳遞數據的幾種方式**
**[點此獲取完整代碼](http://download.csdn.net/detail/u012904198/7312525)**
我們除了要從活動返回數據,也常常要傳遞數據給活動。對此我們可以使用Intent對象將這些數據傳遞給目標活動。
1、創建一個名為PassingData的項目,在activity_main.xml文件中添加一個Button:
~~~
<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" />
~~~
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:onClick="onClick"
android:text="Click to return to main activity" />
~~~
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文件中添加如下代碼:
~~~
<activity
android:name=".SecondActivity"
android:label="Second Activity" >
<intent-filter>
<action android:name="net.zenail.PassingData.SecondActivity" />
<category android:name="android.intent.category.DEFAULT" />
</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應用程序剖析
- (一)——生命周期
- (二)——使用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
- (七)——顯示對話框窗口
- (八)——顯示進度對話框
- (九)——更復雜的進度對話框
- (十)——使用意圖鏈接活動
- (十一)——從意圖返回結果
- (十二)——使用意圖傳遞數據的幾種方式
- (十三)——碎片(一)
- (十四)——在運行時添加碎片(附源碼)
- (十五)——碎片的生命周期(附源碼)
- (十六)——碎片之間進行交互(附源碼)
- (十七)——使用意圖調用內置應用程序
- (十八)——使用意圖篩選器和實現瀏覽網頁(附源碼)