日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問(wèn) 生活随笔!

生活随笔

當(dāng)前位置: 首頁(yè) > 编程资源 > 编程问答 >内容正文

编程问答

enum类型被intent所携带时需要注意的地方

發(fā)布時(shí)間:2025/7/14 编程问答 20 豆豆
生活随笔 收集整理的這篇文章主要介紹了 enum类型被intent所携带时需要注意的地方 小編覺(jué)得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

? ? ? ? 一般我們?cè)贏ctivity之間傳遞對(duì)象時(shí)多用Parcelable。比如寫(xiě)一個(gè)class,在這個(gè)class上標(biāo)明implements Parcelable并實(shí)現(xiàn)接口就可以用Intent.putExtra(String, Parcelable)了。對(duì)于傳遞enum對(duì)象,假設(shè)也采用此方法,即像http://stackoverflow.com/questions/2836256/passing-enum-or-object-through-an-intent-the-best-solution里面3樓說(shuō)的:

?

[java]?view plaincopy
  • public?enum?MyEnum?implements?Parcelable?{??
  • ????VALUE;??
  • ??
  • ????@Override??
  • ????public?int?describeContents()?{??
  • ????????return?0;??
  • ????}??
  • ??
  • ????@Override??
  • ????public?void?writeToParcel(final?Parcel?dest,?final?int?flags)?{??
  • ????????dest.writeInt(ordinal());??
  • ????}??
  • ??
  • ????public?static?final?Creator<MyEnum>?CREATOR?=?new?Creator<MyEnum>()?{??
  • ????????@Override??
  • ????????public?MyEnum?createFromParcel(final?Parcel?source)?{??
  • ????????????return?MyEnum.values()[source.readInt()];??
  • ????????}??
  • ??
  • ????????@Override??
  • ????????public?MyEnum[]?newArray(final?int?size)?{??
  • ????????????return?new?MyEnum[size];??
  • ????????}??
  • ????};??
  • }??
  • ??
  • You?can?than?use?Intent.putExtra(String,?Parcelable).??

  • ??????? 那么我們先定義一個(gè)MyEnum變量a,再調(diào)用intent.putExtra("name", a),會(huì)報(bào)The method putExtra(String, Parcelable) is ambiguous for the type Intent的錯(cuò),為何?

    ?

    因?yàn)閑num自身實(shí)現(xiàn)了Serializable接口,Enum類的源代碼里這么寫(xiě)的:

    ?

    [java]?view plaincopy
  • public?abstract?class?Enum<E?extends?Enum<E>>?implements?Serializable,?Comparable<E>?{??
  • ...?...??
  • }??

  • ??????? 之后你的MyEnum類再實(shí)現(xiàn)Parcelable接口,而intent里面有這樣兩個(gè)函數(shù):Intent.putExtra(String, Parcelable)和Intent.putExtra(String, Serializable),你的MyEnum類實(shí)現(xiàn)了Serializable和Parcelable兩個(gè)接口,在調(diào)用Intent.putExtra時(shí),編譯器就不知道該選Intent.putExtra(String, Parcelable)還是Intent.putExtra(String, Serializable),導(dǎo)致二義性。那么上面的說(shuō)法是否真的無(wú)用?? 其實(shí)這種寫(xiě)法在MyEnum對(duì)象作為類Father1的成員時(shí)還是可以這么寫(xiě)的,我們?cè)贏ctivity間傳Father1,Father1是可以實(shí)現(xiàn)Parcelable接口的。Father1內(nèi)部處理MyEnum成員時(shí)可以這樣:

    ?

    ?

    [java]?view plaincopy
  • private?Father1(Parcel?in)?{??
  • ????mField?=?in.readInt();??
  • ????mMyEnum?=?MyEnum.CREATOR.createFromParcel(in);??
  • }??
  • ??
  • public?void?writeToParcel(Parcel?dest,?int?flags)?{??
  • ????dest.writeInt(mField);??
  • ????mMyEnum.writeToParcel(dest,?flags);??
  • }??
  • ?

    ??????? 經(jīng)測(cè)試,也可以把enum傳遞出去。

    ??????? 既然上述MyEnum的寫(xiě)法可以適應(yīng)enum作為Parcelable類成員來(lái)傳遞,而不能作為單獨(dú)的對(duì)象來(lái)傳遞,要兼得二者該如何做?

    ??????? 首先,MyEnum不需實(shí)現(xiàn)Parcelable接口,單獨(dú)傳遞MyEnum對(duì)象時(shí)就用Intent.putExtra(String, Serializable)。

    ??????? 然后把MyEnum的createFromParcel()和writeToParcel()的代碼整合進(jìn)Father類,代碼片段(變量名有改動(dòng)):

    ?

    [java]?view plaincopy
  • private?Father2(Parcel?in)?{??
  • ????mField?=?in.readInt();??
  • ????mAnotherEnum?=?AnotherEnum.values()[in.readInt()];??
  • }??
  • ??
  • public?static?final?Parcelable.Creator<Father2>?CREATOR?=?new?Parcelable.Creator<Father2>()?{??
  • ??
  • ????public?Father2?createFromParcel(Parcel?in)?{??
  • ????????return?new?Father2(in);??
  • ????}??
  • ??
  • ????@Override??
  • ????public?Father2[]?newArray(int?size)?{??
  • ????????return?new?Father2[size];??
  • ????}??
  • ??
  • };??
  • ??
  • @Override??
  • public?void?writeToParcel(Parcel?dest,?int?flags)?{??
  • ????dest.writeInt(mField);??
  • ????dest.writeInt(mAnotherEnum.ordinal());??
  • }??
  • ??????? 再用Intent.putExtra(String, Parcelable)來(lái)傳Father2對(duì)象就可以了。

    ?

    ??????? 再談一下Enum類的values()方法,這個(gè)方法是無(wú)法通過(guò)eclipse查看到的,它的定義在這里,它返回所有的定義過(guò)的枚舉值,enum的底層實(shí)現(xiàn)就是定義從0到N的數(shù)個(gè)整數(shù),只不過(guò)為每個(gè)整數(shù)取了個(gè)別名,一個(gè)enum變量就是這數(shù)個(gè)整數(shù)之一,這個(gè)enum變量的ordinal()方法就是返回其在這數(shù)個(gè)整數(shù)中的位置。values()靜態(tài)方法返回一個(gè)包含這數(shù)個(gè)整數(shù)的數(shù)組。

    給出示例代碼(免積分下載),在MainActivity.java三處注釋的地方,分別解注釋再運(yùn)行,就會(huì)認(rèn)識(shí)得比較清楚了。

    總結(jié)

    以上是生活随笔為你收集整理的enum类型被intent所携带时需要注意的地方的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。

    如果覺(jué)得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。