问题
我希望将以下属性添加到我在 Android 中构建的自定义视图中:
<attr name="sourceType" format="enum">
<enum name="generic" value="???" />
<enum name="dash" value="???" />
<enum name="smooth_streaming" value="???" />
<enum name="hls" value="???" />
</attr>
在我的代码内部,我想使用枚举来表示各种源类型:
public enum SourceType {
Generic, DASH, SmoothStreaming, HLS;
}
但是在我的自定义视图中,我不确定如何将属性值转换为枚举:
public BFPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.BFPlayer);
// Clearly wrong:
// 1. SourceType.Generic cannot be cast to int
// 2. int cannot be cast to SourceType
SourceType sourceType = attributes.getInt(R.styleable.BFPlayer_sourceType, SourceType.Generic);
}
我试过的
我考虑过执行以下操作:
attrs.xml
<attr name="sourceType" format="enum">
<enum name="generic" value="1" />
<enum name="dash" value="2" />
<enum name="smooth_streaming" value="3" />
<enum name="hls" value="4" />
</attr>
SourceType.java
public enum SourceType {
Generic (1), DASH (2), SmoothStreaming (3), HLS (4);
private int value;
private SourceType(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public static SourceType fromInt(int value) {
switch (value) {
case 1: return Generic;
case 2: return DASH;
case 3: return SmoothStremaing;
case 4: return HLS;
default: throw new Error("Invalid SourceType");
}
}
}
BFPlayer.java
public BFPlayer(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
TypedArray attributes = context.obtainStyledAttributes(attrs, R.styleable.BFPlayer);
// Clearly wrong:
// 1. SourceType.Generic cannot be cast to int
// 2. int cannot be cast to SourceType
SourceType sourceType = SourceType.fromInt(
attributes.getInt(R.styleable.BFPlayer_sourceType, SourceType.Generic.getValue())
);
}
然而,这感觉像是错误的解决方案:
- 它需要使用
.fromtInt并.getValue实例化一个新的 SourceType - 如果我添加新值,它需要更新 switch 语句
- 它为每个选项任意分配整数值
有更好的解决方案吗?