Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android Activity 之間的資料傳遞方式整理

編輯:關於 Android 程式設計

Activity 之間傳遞資料是日常開發的常見需求。本文整理三種主要方式:傳遞基本型別或字串、以 Bundle 打包、以及傳遞自訂物件(實作 Parcelable)。

一、基本知識

1)傳遞基本型別或 String:

intent.putExtra("username", username); String username = intent.getStringExtra("username");

2)以 Bundle 形式傳遞:

Bundle bundle = new Bundle(); bundle.putString("username", username); bundle.putString("password", password); intent.putExtras(bundle); Bundle bundle = intent.getExtras(); String username = bundle.getString("username"); String password = bundle.getString("password");

3)傳遞自訂物件需實作 Parcelable,重寫 describeContents 與 writeToParcel,並提供 CREATOR 與 Parcel 建構子:

public int describeContents() { return 0; } public void writeToParcel(Parcel dest, int flags) { dest.writeInt(id); dest.writeString(name); dest.writeInt(age); } public static final Parcelable.Creator CREATOR = new Parcelable.Creator() { public Person createFromParcel(Parcel in) { return new Person(in); } public Person[] newArray(int size) { return new Person[size]; } }; public Person(Parcel in) { id = in.readInt(); name = in.readString(); age = in.readInt(); } intent.putExtra("person", person); Person person = intent.getParcelableExtra("person");

二、程式碼範例

MainActivity 傳送資料:

package com.example.parsedata; import android.os.Bundle; import android.app.Activity; import android.content.Intent; import android.view.View; import android.widget.EditText; public class MainActivity extends Activity { private EditText et_username; private EditText et_password; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); et_username = (EditText) findViewById(R.id.et_username); et_password = (EditText) findViewById(R.id.et_password); } public void enter(View view) { String username = et_username.getText().toString(); String password = et_password.getText().toString(); Intent intent = new Intent(this, MainActivity2.class); intent.putExtra("username", username); intent.putExtra("password", password); Bundle bundle = new Bundle(); bundle.putString("zhangzetian", "huangjundong"); intent.putExtras(bundle); Person person = new Person(2, "hjd is a handsomeboy..", 21); intent.putExtra("person", person); startActivity(intent); } }

MainActivity2 接收資料:

package com.example.parsedata; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class MainActivity2 extends Activity { private TextView tv_info; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main2); tv_info = (TextView) findViewById(R.id.tv_info); Intent intent = getIntent(); String username = intent.getStringExtra("username"); String password = intent.getStringExtra("password"); Bundle bundle = intent.getExtras(); Person p = intent.getParcelableExtra("person"); tv_info.setText("username: " + username + ",password: " + password + "zhangzetian's husband is : " + bundle.getString("zhangzetian") + " person: " + p.toString()); } }

原始碼下載:https://download.csdn.net/detail/caihongshijie6/7792903

成果示意圖:

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved