1、Intent(意圖):Intent是一種運行時綁定機制(runtime binding),用于在兩個Activity之間傳遞數據。Intent也具有媒體中介的作用,實現調用者與被調用者之間的解耦;
2、Intent傳遞數據通用方式:(1)在Main.java中構造一個Intent;(2)然后調用startActivity(intent)將構造的Intent傳入;(3)系統根據Intent中的描述,到Manifest.xml尋找滿足此要求的Activity(4)系統會調用這個Activity,傳入Intent,執行相應的操作~
3、新建Android項目”android_intent“,創建Activity名為”Main“;
4、在“res/layout/main.xml”中添加一個”Button“,完成布局文件的編寫,代碼如下:
~~~
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="測試使用Intent傳遞數據" >
</Button>
~~~
5、新建“OtherActivity.java”,并在“Superclass”中選擇“android.app.Activity”(繼承Activity),并在其中添加onCreate()方法(代碼在步驟9);
6、接著聲明一個布局文件,在“res/layout”中新建“other.xml”,將其代碼修改如下:
~~~
<?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>
~~~
7、打開“src”中的“Main.java”,在Main類中添加代碼以構造Intent并啟動Intent,添加后代碼如下:
~~~
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);//根據Id查找視圖
button.setOnClickListener(new View.OnClickListener() {//點擊,啟動點擊事件,在其中新建意圖
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//由Main.this傳入OtherActivity.class,表示從當前意圖傳入目標意圖。
Intent intent = new Intent(Main.this, OtherActivity.class);
// 在意圖中傳遞數據,將數據臨時存儲在內存中
intent.putExtra("name", "張三");
intent.putExtra("age", 23);
intent.putExtra("address", "北京");
// 啟動意圖
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;
}
}
~~~
8、打開“AndroidManifest.xml”,在“Application”標簽下添加一個“Activity”標簽(以使系統在傳入Intent后能查找到名為“OtherActivity”的活動),代碼如下:
~~~
<activity android:name=".OtherActivity" >//用“.”表示當前這個類
</activity>
~~~
9、在“OtherActivity.java”中添加一個“getIntent”方法,獲取意圖,隨后調用get方法獲取數據,再將獲取的信息放入“TextView”中。完整代碼如下:
~~~
public class OtherActivity extends Activity {
private TextView textview;
public OtherActivity() {
// TODO Auto-generated constructor stub
}
@Override
protected void onCreate(Bundle savedInstanceState) {//創建Activity
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.other);// 加載布局文件(必須!)
textview = (TextView) this.findViewById(R.id.msg);
//使用getIntent方法獲取意圖
Intent intent = getIntent();
//使用get方法取出Intent中的數據
int age = intent.getIntExtra("age", 0);
String name = intent.getStringExtra("name");
String address = intent.getStringExtra("address");
textview.setText("age--->>" + age + "\n" + "name--->>" + name + "\n"
+ "address--->>" + address + "\n");
}
}
~~~
10、右擊“src”下的包,選擇“Run as”中的“Android Application”來運行程序,結果截圖如下:

點擊“Button”顯示:

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