***************************************
第一種:Extras:額外的,附加的.在Intent中附加額外的消息
//傳值
Intent intent = new Intent(this, XXXActivity.class);
intent.putExtra(key, value);
startActivity(intent);
//取值
getIntent()方法得到intent對象
Intent intent = getIntent();
//獲取Intent中的數據:getXXXExtra()方法
intent.getIntExtra(key, value);--->int
intent.getStringExtra(key);--->String
顯示方式:
吐司:Toast.makeText(this, "", Toast.LENGTH_SHORT).show();
打印:Log.i("TAG", "....");
顯示在TextView控件上:
mTextView.setText();
******************************************************
第二種:Bundle傳值
//傳值:
Intent對象
Intent intent = new Intent(.......);
創建Bundle對象,包裹數據
Bundle bundle = new Bundle();
bundle.putInt(key, value);
bundle.putString(...);
bundle.putBoolean(...);
......
將bundle掛載到Intent對象上
intent.putExtras(bundle);
跳轉頁面
startActivity(intent);
//取值
getIntent()得到intent對象
獲取Bundle對象
intent.getExtras();--->Bundle bundle
bundle.getInt(key);
bundle.getString(key);
...........
顯示:同上
*************************************************
第三種:通過對象方式傳值
//傳值
Intent intent = new Intent(this, XXXActivity.class);
創建一個類實現序列化(Person為例)
部分代碼:
public class Person implements Serializable{
private static final long serialVersionUID = 1L;
private String name;
private List<String> list;
....}
創建Person對象
Person person = new Person();
person.setName(...);
List<String> list = new ArrayList<>();
list.add(...);
person.setList(list);
在intent上設置序列化對象
intent.putExtra(key, person);
跳轉
startActivity(intent);
//取值
獲取intent對象
getIntent();-->Intent intent
獲取序列化對象
intent.getSerializableExtra(key);-->Person person
顯示在TextView上:
mTextView.setText(person.toString());