Intent意圖 可用於Activity之間的數據傳遞,一般可分為下面兩種情況,從當前Activity傳遞到目標Activity後有無返回值:
1.傳遞後無返回值的情況:
在起始Activity中,發送數據
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.thisactivity);
Intent intent = new Intent();
//設置起始Activity和目標Activity,表示數據從這個Activity傳到下個Activity
intent.setClass(ThisActivity.this,TargetActivity.class);
//綁定數據
intent.putExtra("username",username);//也可以綁定數組
intent.putExtra("userpass",userpass);
//打開目標Activity
startActivity(intent);
}
在目標Activity中,接收數據:
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.targetactivity);
//獲得意圖
Intent intent = getIntent();
//讀取數據
String name = intent.getStringExtra("username");
String pass = intent.getStringExtra("userpass);
}
也可以適用Bundle(捆)
在起始Activity中,發送數據:
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.thisactivity);
Intent intent = new Intent();
//設置起始Activity和目標Activity,表示數據從這個Activity傳到下個Activity
intent.setClass(ThisActivity.this,TargetActivity.class);
//一次綁定多個數據
Bundle bundle = new Bundle();
bundle.putString("username",username);
bundle.putString("userpass",userpass);
intent.putExtras(bundle);
//打開目標Activity
startActivity(intent);
}
在目標Activity中,接收數據:
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.targetactivity);
//獲得意圖
Intent intent = getIntent();
//讀取數據
Bundle bundle = intent.getExtras();
String name = bundle.getString("username");
String pass = bundle.getString("userpass");
}
2.傳遞後有返回值的情況:
當需要從目標Activity回傳數據到原Activity時,可以使用上述方法定義一個新的Intent來傳遞數據,也可以使用startActivityForResult (Intent intent, int requestCode);方法。
在起始Activity中,發送數據:
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.thisactivity);
Intent intent = new Intent();
//設置起始Activity和目標Activity,表示數據從這個Activity傳到下個Activity
intent.setClass(ThisActivity.this,TargetActivity.class);
//綁定數據
intent.putExtra("username",username);//也可以綁定數組
intent.putExtra("userpass",userpass);
//打開目標Activity
startActivityForResult(intent,1);
}
//需要重寫onActivityResult方法
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode,resultCode,intent);
//判斷結果碼是否與回傳的結果碼相同
if(resultCode == 1){
//獲取回傳數據
String name = intent.getStringExtra("name");
String pass = intent.getStringExtra("pass);
//對數據進行操作
......
}
在目標Activity中,接收數據:
protected void onCreate(Bundle saveInstanceState){
super.onCreate(saveInstanceState);
setContentView(R.layout.targetactivity);
//獲得意圖
Intent intent = getIntent();
//讀取數據
String name = intent.getStringExtra("username");
String pass = intent.getStringExtra("userpass);
//從EditText中獲取新的數據給name和pass
name = editText1.getText().toString();
pass = editText2.getText().toString()
//數據發生改變,需要把改變後的值傳遞回原來的Activity
intent.putExtra("name",name);
intent.putExtra("pass",pass);
//setResult(int resultCode,Intent intent)方法
setResult(1,intent);
//銷毀此Activity,摧毀此Activity後將自動回到上一個Activity
finish();
}
更多Android相關信息見Android 專題頁面 http://www.linuxidc.com/topicnews.aspx?tid=11