(Android review)Activity之間的資料傳遞,androidactivity

來源:互聯網
上載者:User

(Android review)Activity之間的資料傳遞,androidactivity


一、基本知識點

1、Activity之間傳遞資料
1)傳遞基本類型或String
intent.putExtra("username", username);
 
 getIntent();
 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)傳遞自訂類型


1))implements Parcelable
2))重寫以下兩個方法
public int describeContents() {
// TODO Auto-generated method stub
return 0;
}

//把資料寫入到Parcel對象
public void writeToParcel(Parcel dest, int flags) {
// TODO Auto-generated method stub
dest.writeInt(id);
dest.writeString(name);
dest.writeInt(age);
}


3))拷貝以下方法(把Person換成你的實際類型)


 public static final Parcelable.Creator<Person> CREATOR
   = new Parcelable.Creator<Person>() {
    //從Parcel對象裡面讀取資料
public Person createFromParcel(Parcel in) {
   return new Person(in);
}

public Person[] newArray(int size) {
   return new Person[size];
}
};

public Person(Parcel in) {
// TODO Auto-generated constructor stub
id = in.readInt();
name = in.readString();
age = in.readInt();
}




Person person = new Person(100, "張信哲", 40);
    intent.putExtra("person", person);




Person person = intent.getParcelableExtra("person");


二、代碼

1、MainActivity

package com.example.parsedata;import android.os.Bundle;import android.app.Activity;import android.content.Intent;import android.view.Menu;import android.view.View;import android.widget.EditText;public class MainActivity extends Activity {private EditText et_username;private EditText et_password;@Overrideprotected 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);}@Overridepublic 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;}}


2、MainActivity2

package com.example.parsedata;import android.app.Activity;import android.content.Intent;import android.os.Bundle;import android.view.View;import android.widget.EditText;import android.widget.TextView;public class MainActivity2 extends Activity {private TextView tv_info;@Overrideprotected 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());}}


三、源碼下載:

http://download.csdn.net/detail/caihongshijie6/7792903


四、





【Android 開發】 : Activity之間傳遞資料的幾種方式

1. 使用Intent來傳遞資料Intent表,很多時候我們都會利用Android的Intent來在各個Activity之間傳遞資料,這也是Android比較官方的一種資料傳遞的方式需求1:從一個Activity(IntentDemo)跳轉到另外一個Activity(Other),其中利用Intent來傳遞資料程式Demo如下:IntentDemo.javapackage com.android.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.View; import android.widget.Button; public class IntentDemo extends Activity { private Button button; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); initComponent(); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(IntentDemo.this, Other.class); // 在Intent中傳遞資料 intent.putExtra("name", "AHuier"); intent.putExtra("age", 22); intent.putExtra("address", "XiaMen"); // 啟動Intent startActivity(intent); } }); } private void initComponent() { button = (Button) findViewById(R.id.button); } } other.javapackage com.android.intentdemo; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.widget.TextView; public class Other extends Activity { private TextView textView; @Override protected void onCreate(Bundle savedInstanceState) { // TODO Auto-generated method stub super.onCreate(savedInstanceState); setContentView(R.layout.other); initComponent(); Intent intent = getIntent(); int age = intent.getIntExtra(......餘下全文>>
 
android activity 間傳遞自訂類型資料怎傳輸

可以用全域變數來解決。
1、自訂一個Application的衍生類別MyApplication,然後在Manifest中指定MyApplication為你的application name。
2、在MyApplication中定義你要傳遞資料類型對應的變數。然後增加一個成員函數,用於獲得該變數的引用。
3、當然對MyApplication中對應變數的讀寫要做好同步。
---------------------------------------------------------------------
更多疑問解答,請關注@安卓互助平台 新浪微博
 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.