欧美一区二区三区老妇人-欧美做爰猛烈大尺度电-99久久夜色精品国产亚洲a-亚洲福利视频一区二区

怎么使用Intent傳遞對象

這篇文章主要介紹“怎么使用Intent傳遞對象”,在日常操作中,相信很多人在怎么使用Intent傳遞對象問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”怎么使用Intent傳遞對象”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

目前創(chuàng)新互聯(lián)公司已為數(shù)千家的企業(yè)提供了網(wǎng)站建設(shè)、域名、網(wǎng)站空間、網(wǎng)站改版維護(hù)、企業(yè)網(wǎng)站設(shè)計(jì)、龍山網(wǎng)站維護(hù)等服務(wù),公司將堅(jiān)持客戶導(dǎo)向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

Intent的用法以及非常簡單了,可以利用它來啟動活動,發(fā)送廣播,啟動服務(wù)等,當(dāng)然,也可以用來傳值

Intent intent=new Intent(OneActivity.this,TwoActivity.class);
intent.putExtra("string_data","hello");
startActivity(intent);

但是,當(dāng)我們想要傳遞一些自定義的對象的時候,好像就有點(diǎn)無從下手了

這就要說到傳遞對象的兩種方式:Serializable和Parcelable

Serializable是序列化的意思,將一個對象轉(zhuǎn)換成礦業(yè)存儲或者可以傳輸?shù)臓顟B(tài),序列化后的對象可以在網(wǎng)絡(luò)上進(jìn)行傳輸,也可以存儲到本地。而序列化的方法也很簡單,只需要讓一個類去實(shí)現(xiàn)Serializable這個接口就行了

比如說有一個Apple類

public class Apple implements Aerializable{
private Stirng name;
private int price;
public String getName(){
return name;
}
public void setName(name){
this.name=name;
}
public String getPrice(){
return price;
}
public void setPrice(int price){
this.price=price;
}
}

然后在FirstActivity中也和簡單

Apple apple=new Apple();
apple.setName=("紅富士");
apple.setprice=(10);
Intent intent=new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("Apple_data",apple);
startActivity(intent);

而獲取這個對象也很簡單

Apple apple=(Apple) getIntent.getSerializableExtra(apple_data);

Parcelable也可以實(shí)現(xiàn)相同的效果,不過不同于將對象進(jìn)行序列化,Parcelable方法的實(shí)現(xiàn)原理是將一個完整的對象進(jìn)行分解,而分解后的每一部分都是Intent所支持的數(shù)據(jù)類型

public class Apple implements Parcelable{
private Stirng name;
private int price;
public String getName(){
return name;
}
public void setName(name){
this.name=name;
}
public String getPrice(){
return price;
}
public void setPrice(int price){
this.price=price;
}
@Override
public int describeContents(){
return 0;
}
@Override
public void writeToParcel(Parcel dest,int flags){
dest.writeString(name);
dest.writeInt(price);
}
public static final Parcelable.Creator<Person> CREATOR =new Parcelable.Creator<Person>(){
@Override
public Apple creatFromParcel(Parcel source){
Apple apple=new Apple();
apple.name=source.readString();
apple.price=source.readInte();
return apple;
}
@Override
public Apple[] newArray(int size){
return new Person[size];
}
}
}

在FirstActivity中的操作和Serializable一樣

接受為

Apple apple=(Apple) getIntent.getParcelableExtra(apple_data);

到此,關(guān)于“怎么使用Intent傳遞對象”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實(shí)踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注創(chuàng)新互聯(lián)網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬?shí)用的文章!

當(dāng)前題目:怎么使用Intent傳遞對象
網(wǎng)站鏈接:http://chinadenli.net/article14/ggjige.html

成都網(wǎng)站建設(shè)公司_創(chuàng)新互聯(lián),為您提供商城網(wǎng)站定制網(wǎng)站網(wǎng)站策劃網(wǎng)站導(dǎo)航網(wǎng)站制作全網(wǎng)營銷推廣

廣告

聲明:本網(wǎng)站發(fā)布的內(nèi)容(圖片、視頻和文字)以用戶投稿、用戶轉(zhuǎn)載內(nèi)容為主,如果涉及侵權(quán)請盡快告知,我們將會在第一時間刪除。文章觀點(diǎn)不代表本網(wǎng)站立場,如需處理請聯(lián)系客服。電話:028-86922220;郵箱:631063699@qq.com。內(nèi)容未經(jīng)允許不得轉(zhuǎn)載,或轉(zhuǎn)載時需注明來源: 創(chuàng)新互聯(lián)

h5響應(yīng)式網(wǎng)站建設(shè)