1、使用Intent可以很方便地在不同的Activity間傳遞數據,這個也是官方推薦的方式,但是也有一定的局限性,就是Intent無法傳遞不能序列化的對象,然而這個問題可以用靜態變量來解決~
2、下面來具體舉個例子,新建一個Android工程,如下圖:

3、在布局文件(“res/layout”)中添加按鈕“Button”,代碼如下:
~~~
<Button
android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="使用靜態變量傳遞數據" />
~~~
4、在當前目錄下再建一個布局文件“other.xml”,在其中添加一個“TextView”標簽,代碼如下:
~~~
<?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:id="@+id/msg"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</TextView>
</LinearLayout>
~~~
5、右擊“src”下的包,新建類“OtherActivity”并使其繼承“Activity”,在這個類中添加“onCreate”方法,并在其中添加成員“textview”、“name”、“age”,最后將其值設置給“textview”,代碼如下:
~~~
package com.android.myintent;
import android.app.Activity;
import android.os.Bundle;
import android.widget.TextView;
public class OtherActivity extends Activity {
private TextView textview; // 添加一個靜態變量
// 欲傳遞數據,添加一個public屬性的靜態變量
public static String name;
public static int age;
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);// 加載布局文件
textview = (TextView) this.findViewById(R.id.msg);// 查找并加載
textview.setText("-name-->>" + name + "\n" + "-age-->>" + age);
}
public OtherActivity() {
// TODO Auto-generated constructor stub
}
}
~~~
6、在“Main.java”里添加一個“Button”類型成員,設置點擊事件,并創建意圖為OtherActivity中的靜態變量賦值,代碼如下:
~~~
package com.android.myintent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
public class Main extends Activity {
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = (Button) this.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
// 聲明一個意圖
Intent intent = new Intent();// 可以構造一個空意圖,在下面傳遞類
intent.setClass(Main.this, OtherActivity.class);
OtherActivity.age = 23;
OtherActivity.name = "Jack";
startActivity(intent);
}
});
}
@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、在“AndroidManifest.xml”中添加一個“activity.xml”設置,“name”為“.OtherActivity”,使系統能找到該類,代碼:
~~~
<activity android:name=".OtherActivity" >
</activity>
~~~
8、運行程序,結果截圖如下:


Ps:My second Android Application~
- 前言
- 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
- (七)——顯示對話框窗口
- (八)——顯示進度對話框
- (九)——更復雜的進度對話框
- (十)——使用意圖鏈接活動
- (十一)——從意圖返回結果
- (十二)——使用意圖傳遞數據的幾種方式
- (十三)——碎片(一)
- (十四)——在運行時添加碎片(附源碼)
- (十五)——碎片的生命周期(附源碼)
- (十六)——碎片之間進行交互(附源碼)
- (十七)——使用意圖調用內置應用程序
- (十八)——使用意圖篩選器和實現瀏覽網頁(附源碼)