那實(shí)際開發(fā)中肯定是要用Java枚舉的进肯,那有沒(méi)有什么解決辦法了岂傲。答案肯定是有的痰洒,只是換成另外一種方式
/**
* Android枚舉
*/
public class TestEnum {
/**
* @Retention(RetentionPolicy.SOURCE) 注解可以告知編譯器不將枚舉的注解數(shù)據(jù)存儲(chǔ)在 .class 文件中
*
*/
/**
* @StringDef創(chuàng)建整型和字符串集的枚舉注解來(lái)驗(yàn)證其他類型的代碼引用
* 具體詳細(xì)看官網(wǎng){@link https://developer.android.com/studio/write/annotations.html?hl=zh-cn#adding-annotations}
*/
@Retention(RetentionPolicy.SOURCE)
@StringDef({
POWER_SERVICE,
WINDOW_SERVICE,
LAYOUT_INFLATER_SERVICE
})
//@interface這是Java用來(lái)定義一個(gè)注解類。
public @interface ServiceName {}
public static final String POWER_SERVICE = "power";
public static final String WINDOW_SERVICE = "window";
public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater";
public static String getSystemService(@ServiceName String name) {
if (POWER_SERVICE.equals(name)) {
return POWER_SERVICE;
} else if (WINDOW_SERVICE.equals(name)) {
return WINDOW_SERVICE;
} else if (LAYOUT_INFLATER_SERVICE.equals(name)) {
return LAYOUT_INFLATER_SERVICE;
}
return null;
}
public static void main(String[] args) {
//使用
System.out.println(TestEnum.getSystemService(TestEnum.LAYOUT_INFLATER_SERVICE));
}
}
這就是官網(wǎng)的寫法了瘟裸】筒妫可是發(fā)現(xiàn)每次傳參數(shù)是
TestEnum.LAYOUT_INFLATER_SERVICE
或者
TestEnum.POWER_SERVICE
而不是像我們Java枚舉的傳參方式
ServiceName.LAYOUT_INFLATER_SERVICE
那是不是需要改造下。查看資料可以這樣寫
/**
* Android枚舉
*/
public class TestEnum {
/**
* @Retention(RetentionPolicy.SOURCE) 注解可以告知編譯器不將枚舉的注解數(shù)據(jù)存儲(chǔ)在 .class 文件中
*
*/
/**
* @StringDef創(chuàng)建整型和字符串集的枚舉注解來(lái)驗(yàn)證其他類型的代碼引用 具體詳細(xì)看官網(wǎng){@link https://developer.android.com/studio/write/annotations.html?hl=zh-cn#adding-annotations}
*/
@Retention(RetentionPolicy.SOURCE)
@StringDef({
ServiceName.POWER_SERVICE,
ServiceName.WINDOW_SERVICE,
ServiceName.LAYOUT_INFLATER_SERVICE
})
//@interface這是Java用來(lái)定義一個(gè)注解類话告。
public @interface ServiceName {
String POWER_SERVICE = "power";
String WINDOW_SERVICE = "window";
String LAYOUT_INFLATER_SERVICE = "layout_inflater";
}
public static String getSystemService(@ServiceName String name) {
if (ServiceName.POWER_SERVICE.equals(name)) {
return ServiceName.POWER_SERVICE;
} else if (ServiceName.WINDOW_SERVICE.equals(name)) {
return ServiceName.WINDOW_SERVICE;
} else if (ServiceName.LAYOUT_INFLATER_SERVICE.equals(name)) {
return ServiceName.LAYOUT_INFLATER_SERVICE;
}
return null;
}
public static void main(String[] args) {
//使用
System.out.println(TestEnum.getSystemService(ServiceName.LAYOUT_INFLATER_SERVICE));
}
}
不錯(cuò)兼搏,就是把常量放到里面去了。這個(gè)時(shí)候差不多好了沙郭,不過(guò)怎么看
public static String getSystemService(@ServiceName String name) {
if (ServiceName.POWER_SERVICE.equals(name)) {
return ServiceName.POWER_SERVICE;
} else if (ServiceName.WINDOW_SERVICE.equals(name)) {
return ServiceName.WINDOW_SERVICE;
} else if (ServiceName.LAYOUT_INFLATER_SERVICE.equals(name)) {
return ServiceName.LAYOUT_INFLATER_SERVICE;
}
return null;
}
這個(gè)代碼還是有點(diǎn)怪怪的佛呻,每次添加一個(gè)值都需要加一個(gè)else if太麻煩了。在Java中可以通過(guò)enum.values()獲取所有enum的值那是不是有類似的方法病线,找了資料好像沒(méi)有發(fā)現(xiàn)吓著,那只能通過(guò)反射獲取所有的public的參數(shù)和參數(shù)值了
public static String getSystemService(@ServiceName String name) {
Field[] fields = ServiceName.class.getDeclaredFields();
for (Field field : fields) {
//在反射對(duì)象中設(shè)置 accessible 標(biāo)志允許具有足夠特權(quán)的復(fù)雜應(yīng)用程序(比如 Java Object Serialization 或其他持久性機(jī)制)以某種通常禁止使用的方式來(lái)操作對(duì)象
field.setAccessible(true);
String value = null;
try {
value = String.valueOf(field.get(field.getName()));
} catch (IllegalAccessException e) {
e.printStackTrace();
}
if (value != null && value.equals(name)) {
return value;
}
}
return null;
}
ok完成,代碼地址
這里有幾個(gè)知識(shí)點(diǎn)參考資料鏈接