在Activity之間進行數據交換有一個“信使”:Intent,因此主要是將需要交換的數據放入Intent即可
Intent提供了多個重載方法,“攜帶”額外的數據:
**放入Bundle數據:**
putExtras(Bundle data);向Intent中放入需要“攜帶”數據包
Bundle getExtras();取出Intent所“攜帶”的數據包
**放入鍵值數據:**(源碼底層實際上還是使用的Bundle)
putExtra(String name,Xxx value);按key-value形式放入數據,Xxx是任意類型
getXxxExtra(String name);按key值取出指定類型的數據


Bundle就是一個簡單的數據攜帶包,Bundle方法:
**放入基本類型:**
putXxx(String key,Xxx data);向Bundle中放入Int,Long等各種類型的數據
getXxx(String key);取出Int,Long等各種類型數據
**放入對象**:
putSeralizable(String key,Seralizable data);放入一個可序列化的對象
getSeralizable(String key,Seralizable data);取出一個可序列化的對象

一般第一個Activity中:
* Bundle放數據用:
* putXxx(String key,Xxx value);想Bundle中放入int,Long等簡單數據類型
* putSerializable(String key,Serializable data);想Bundle中放入可序列化的對象,主要用于放對象
* Intent放Bundle用:
* putExtras(Bundle data):向Intent中放入需要“攜帶”的數據包
被啟動的Activity中:
* Intent取數據:
* Bundle getExtras();取出Intent所“攜帶”的數據包
* getXxxExtras(String key);從Intent中按key取出指定類型的數據,便攜方式取Bundle中的數據
用第二個Activity處理注冊信息:
activity_main.xml
~~~
<?xml version="1.0" encoding="utf-8"?>
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="請輸入您的注冊信息"
android:textSize="20dp" />
<TableRow
android:id="@+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="賬號:"
android:textSize="20dp" />
<EditText
android:id="@+id/name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="請輸入賬號"
android:selectAllOnFocus="true" >
</EditText>
</TableRow>
<TableRow
android:id="@+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="密碼:"
android:textSize="20dp" />
<EditText
android:id="@+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:hint="請輸入密碼"
android:inputType="textPassword" />
</TableRow>
<TableRow
android:id="@+id/tableRow3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="性別:"
android:textSize="20dp" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radio0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="男" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="女" />
</RadioGroup>
</TableRow>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="注冊" />
</TableLayout>
~~~

MainActivity.java
~~~
package com.hust.bundletest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;
/*用第二個Activity處理注冊信息。
* 在Activity之間進行數據交換是同過Intent信使來實現的。Intent提供了多種重載的方法,來攜帶額外數據:
* 1,使用Bundle對象捆綁數據,Intent攜帶Bundle對象
* putExtras(Bundle data):向Intent中放入需要“攜帶”的數據包
* Bundle getExtras();取出Intent所“攜帶”的數據包
* 2,對于簡單的數據類型,可以使用便攜的方法直接存,取Intent所攜帶的Bundle對象,源碼里可發現都是先創建的Bundle對象
* putExtras(String key,Xxx value);想Intent中按鍵值對(key-value)的形式存入數據
* getXxxExtras(String key);從Intent中按key取出指定類型的數據
* */
/*
* Bundle就是一個簡單的數據攜帶包,該Bundle對象包含了多種方法:
* 1,放入簡單的數據類型
* putXxx(String key,Xxx value);想Bundle中放入int,Long等簡單數據類型
* getXxx(String key);取出int,long個中簡單的數據類型
*2,放入可序列化的對象,實現Seriliable接口的對象
* putSerializable(String key,Serializable data);想Bundle中放入可序列化的對象
* getSerializable(String key);取出可序列化的對象
*
* 一般第一個Activity中:
* Bundle放數據用:
* putXxx(String key,Xxx value);想Bundle中放入int,Long等簡單數據類型
* putSerializable(String key,Serializable data);想Bundle中放入可序列化的對象,主要用于放對象
* Intent放Bundle用:
* putExtras(Bundle data):向Intent中放入需要“攜帶”的數據包
*
* 被啟動的Activity中:
* Intent取數據:
* Bundle getExtras();取出Intent所“攜帶”的數據包
* getXxxExtras(String key);從Intent中按key取出指定類型的數據,便攜方式取Bundle中的數據
* */
public class MainActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn=(Button) findViewById(R.id.button1);
btn.setOnClickListener(new OnClickListener(){
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
//獲得的三個組件
EditText name=(EditText) findViewById(R.id.name);
EditText password=(EditText) findViewById(R.id.password);
RadioButton male=(RadioButton) findViewById(R.id.radio0);
//判斷是否被選
String sex=(male.isChecked())?"男":"女";
//封裝成一個對象
Person p=new Person(name.getText().toString(),password.getText().toString(),sex);
//創建Bundle對象
Bundle bundle=new Bundle();
bundle.putSerializable("person", p);
//創建一個Intent對象
Intent intent=new Intent(MainActivity.this,ResultActivity.class);
intent.putExtras(bundle);
//啟動intent對應的Activity
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;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
~~~
Person.java
~~~
package com.hust.bundletest;
import java.io.Serializable;
public class Person implements Serializable {
String name;
String password;
String sex;
public Person(String name, String password, String sex) {
super();
this.name = name;
this.password = password;
this.sex = sex;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getSex() {
return sex;
}
public void setSex(String sex) {
this.sex = sex;
}
}
~~~
ResultActivity.java
~~~
package com.hust.bundletest;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.TextView;
public class ResultActivity extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_result);
//獲取顯示組件
TextView name=(TextView) findViewById(R.id.text1);
TextView password=(TextView) findViewById(R.id.text2);
TextView sex=(TextView) findViewById(R.id.text3);
//獲取Intent對象
Intent intent=getIntent();
//從Intent對象中獲取序列數據
//Person p=(Person) intent.getSerializableExtra("person");相當于
Bundle bundle=intent.getExtras();//獲取Bundle對象
Person p=(Person) bundle.getSerializable("person");//Bundle對象中獲取可序列化對象
name.setText(p.getName());
password.setText(p.getPassword());
sex.setText(p.getSex());
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.result, menu);
return true;
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
~~~

- 前言
- Eclipse搭建android環境及Genymotion模擬器安裝問題解決方法
- 表格布局(TableLayout)及重要屬性
- 幀布局(FrameLayout)及屬性
- layout_width和width,layout_height和height
- UI組件之TextView及其子類
- UI組件之TextView及其子類(一)TextView和EditText
- UI組件之TextView及其子類(二)RadioButton和CheckBox
- UI組件之TextView及其子類(三)ToggleButton和Switch
- UI組件之TextView及其子類(四)AnalogClock,DigitalClock
- UI組件之TextView及其子類(五)計時器Chronometer
- UI組件之ImageView及其子類(一)ImageView顯示圖片
- UI組件之ImageView及其子類(二)ImageButton ,ZoomButton
- UI組件之AdapterView及其子類關系,Adapter接口及其實現類關系
- UI組件之AdapterView及其子類(一)三種Adapter適配器填充ListView
- UI組件之AdapterView及其子類(二)GridView網格視圖的使用
- UI組件之AdapterView及其子類(三)Spinner控件詳解
- UI組件之AdapterView及其子類(四)Gallery畫廊控件使用
- UI組件之AdapterView及其子類(五)ListView組件和ListActivity
- UI組件之AdapterView及其子類(六)ExpandableListView組件和ExpandableListActivity的使用
- UI組件之 ProgressBar及其子類(一)ProgressBar進度條的使用
- UI組件之ProgressBar及其子類(二)SeekBar拖動條和RatingBar星級評分條的使用
- ViewFlipper的功能和用法
- Toast的功能和用法
- TabHost選項卡的 功能和用法
- AlertDialog創建6種對話框的用法
- Android基于監聽的事件處理機制
- Android基于回調的事件處理
- Handler消息傳遞機制(一)
- Handler消息傳遞機制(二)Handler,Loop,Message,MessageQueue的工作原理
- 啟動Activity的兩種方式startActivity和startActivityForResult(一)
- 啟動Activity的兩種方式startActivity和startActivityForResult(二)
- Activity的生命周期理解
- Bundle在Activity之間交換數據
- 通過 Intent 傳遞類對象
- Intent對象詳解(一)
- Intent對象詳解(二)
- 使用指定的Action,Category調用系統Activity
- 使用Action,Data屬性啟動系統Activity
- Android數據存儲的三種方式-SharedPrefrences,File,SQLite