我将attributeset添加到这样的自定义组件:
我可以通过从类型化数组中询问它来获取每个枚举的值:
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();
}
但是,如果我想要枚举的实际名称,而不是值,该怎么办?我怎么能在代码中得到它?请帮忙
UPDATE
我这样做的主要原因是我可以在不同的情况下重用这个组件,并对layout xml中定义的每个环境有不同的规则,如下所示:
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">
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"/>
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"/>
如您所见,每个组件“MyseekBarWidget”都有不同的类型(百分比与金钱).这将触发不同的规则.
但是当我打电话找出类型时,我只得到枚举的值.我想要这个名称,以便更清晰的代码.我可以写:
if(type==money)...;
与写作:
if(type==1)..;
这使代码更难调试/理解