android enum 定义,android – 属性集,按名称获取枚举

我将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)..;

这使代码更难调试/理解

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值