我正在向自定义组件添加属性集,如下所示:
<declare-styleable name="MySeekBarWidget">
<attr name="type">
<enum name="money" value="1" />
<enum name="percent" value="2" />
<enum name="time" value="3" />
<enum name="money_frequency" value="4" />
<enum name="money_percent_or_fixed" value="5" />
<enum name="money_month" value="6" />
<enum name="time_year_only" value="7" />
</attr>
</declare-styleable>
我可以通过从类型化数组中询问来获取每个枚举的值:
public MySeekBarWidgetLayout(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.MySpinnerContainer);
type = array.getInt(R.styleable.MySeekBarWidget_type,1);
array.recycle();
}
但是如果我想要枚举的实际名称,而不是值呢?我如何在代码中得到它?请帮忙
更新 我这样做的主要原因是我可以在不同的情况下重用这个组件,并且在布局 xml 中为每种情况定义不同的规则,如下所示:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:app1="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<view
android:id="@+id/autoCalculatorAmountMySeekBarWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/autoCalculatorMyDateView"
android:layout_marginLeft="@dimen/side_margin"
android:layout_marginRight="@dimen/side_margin"
android:layout_marginTop="@dimen/top_spacing"
android:tag="@string/amount"
app:max="@integer/a_million"
app:type="money"
class="customComponents.MySeekBarWidget"/>
<view
android:id="@+id/autoCalculatorInterestMySeekBarWidget"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/autoCalculatorAmountMySeekBarWidget"
android:layout_marginLeft="@dimen/side_margin"
android:layout_marginRight="@dimen/side_margin"
android:layout_marginTop="@dimen/top_spacing"
android:tag="@string/interest_rate"
app:max="@integer/fifty"
app:type="percent"
class="customComponents.MySeekBarWidget"/>
</RelativeLayout>
如您所见,“MyseekBarWidget”的每个组件都有不同的类型(百分比与金钱)。这将触发不同的规则。 但是当我调用查找类型时,我只得到枚举的值。我想要这个名字,这样它可以使代码更清晰。我可以写:
if(type==money)...;
与写作相比:
if(type==1)..;
这使得代码更难调试/理解
最佳答案
我肯定会采用 Android 团队一直在做的相同方式:在您的 MySeekBarWidget
类中定义常量,就像 this one .
所以在你的情况下,你会定义:
class MySeekBarWidget... {
public static final int TYPE_MONEY = 1;
public static final int TYPE_PERCENT = 2;
public static final int TYPE_TIME = 3;
...
谁谁谁。之后,很容易引用那些:
type = array.getInt(R.styleable.MySeekBarWidget_type, TYPE_MONEY);
TYPE_
前缀就在那里,以防您想将它们与其他样式分开。事实上,您可以只对这些使用枚举:
class MySeekBarWidget... {
public enum Type {
MONEY(1),
PERCENT(2),...
TIME_YEAR_ONLY(7);
private final int id;
Type(int id) {
this.id = id;
}
public int getValue() {
return id;
}
}
那么,它可能是这样的:
type = array.getInt(R.styleable.MySeekBarWidget_type, Type.MONEY);
并且从外部访问变得非常干净:
int outsideType = MySeekBarWidget.Type.MONEY;
之后,您还可以通过简单地执行以下操作来检查打开的是什么类型:
if (type == Type.MONEY) {
...
} else if (type == Type.PERCENT) {
...
}
或者更好的是,为了避免重复,Enum
可以用在 switch
语句中:
switch (type) {
case Type.MONEY:
...
break;
case Type.PERCENT:
...
break;
}
这减少了 type ==
的重复,并提供了一种向条件添加新类型的简洁方法。
关于android - 属性集,通过名称获取枚举,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24544427/