Android中資料的傳遞

來源:互聯網
上載者:User

標籤:des   his   contents   res   類型   receive   需要   ring   turn   

Android中當兩個Activity需要有資訊互動的時候,可以使用Intent。具體來說:

發送單一類型資料:

發送方:String data = "Hello SecondActivity";Intent intent = new Intent(FirstActivity.this, SecondActivity.class);intent.putExtra("extra_data", data);startActivity(intent); #在onClick中調用接收方:Intent  intent = getIntent();String data = intent.getStringExtra("extra_data");#可以在onCreate中讀取

除了putExtra("key", value)/getStringExtra("key")之外, 根據傳遞的value的類型還有很多對,比如:

putExtra("key", intvalue)/getintExtra("key")putExtra("key", floatvalue)/getfloatExtra("key")putExtra("key", doublevalue)/getDoubleExtra("key")
putExtra("key", bundlevalue)/getBundleExtra("key")

...

發送多種資料類型的組合:

也可以用Bundle把很多資料類型(包括嵌套Bundle)放到Bundle中一併發送:

//sender
Bundle b = new Bundle();b.putInt("age", 2);b.putString("data", "hello");intent.putExtras(b);startActivity(intent);
//receiver
Intent i = getIntent();Bundle b = i.getExtras();int age = b.getInt("age");String data = b.getString("data");

發送類對象:

方法1:Serializable

Serializable是Java提供的序列化方法,優點是使用簡單,一切由系統負責;缺點是速度稍慢。

public class User implements Serializable{    private String name;    private Integer age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public User(String name, Integer age){        this.name = name;        this.age = age;    }}
//sender
intent.putExtra("user", (Serializable) new User("JM", 30));
//ReceiverIntent i = getIntent();User user = (User) i.getSerializableExtra("user");textView.setText(String.format("name: %s, age: %s", user.getName(), user.getAge()));

方法2:Parcelable

public class User implements Parcelable{    private String name;    private Integer age;    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public User(String name, Integer age){        this.name = name;        this.age = age;    }    @Override    public int describeContents() {        return 0;    }    @Override    public void writeToParcel(Parcel parcel, int i) {        //write all the fields that want to send        parcel.writeString(getName());        parcel.writeInt(getAge());    }    public static final Creator<User> CREATOR = new Creator<User>() {        @Override        public User createFromParcel(Parcel parcel) {            return new User(parcel.readString(), parcel.readInt());        }        @Override        public User[] newArray(int i) {            return new User[i];        }    };}
//Sender            intent.putExtra("user", (Parcelable) new User("JM", 30));                startActivity(intent);
//Receiver        User user = (User) i.getParcelableExtra("user");        textView.setText(String.format("name: %s, age: %s", user.getName(), user.getAge()));

如果在User中有多個同類型欄位,比如String,可以用Bundle來實現:

@Override    public void writeToParcel(Parcel parcel, int i) {        //write all the fields that want to send//        parcel.writeString(getName());//        parcel.writeInt(getAge());        Bundle b = new Bundle();        b.putString("name", getName());        b.putInt("age", getAge());        parcel.writeBundle(b);    }    public static final Creator<User> CREATOR = new Creator<User>() {        @Override        public User createFromParcel(Parcel parcel) {//            return new User(parcel.readString(), parcel.readInt());            Bundle b = parcel.readBundle();            return new User(b.getString("name"), b.getInt("age"));        }        @Override        public User[] newArray(int i) {            return new User[i];        }    };

從別的Activity得到返回結果:

啟動端:

startActivityForResult(intent, 0);

0表示請求碼,說明此次請求的意圖是什麼。

接收端:

        Intent i = new Intent();        i.putExtra("data", editText.getText().toString());        setResult(RESULT_OK, i);        finish();

 

Android中資料的傳遞

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.