属性集,按名称获取枚举

jla*_*eux 6 xml enums android

我将attributeset添加到这样的自定义组件:

<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>
Run Code Online (Sandbox Code Playgroud)

我可以通过从类型化数组中询问它来获取每个枚举的值:

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();

    }
Run Code Online (Sandbox Code Playgroud)

但是,如果我想要枚举的实际名称,而不是值,该怎么办?我怎么能在代码中得到它?请帮忙

更新 我这样做的主要原因是我可以在不同的情况下重用这个组件,并对layout 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>
Run Code Online (Sandbox Code Playgroud)

如您所见,每个组件"MyseekBarWidget"都有不同的类型(百分比与金钱).这将触发不同的规则.但是当我打电话找出类型时,我只得到枚举的值.我想要这个名称,以便更清晰的代码.我可以写:

if(type==money)...;
Run Code Online (Sandbox Code Playgroud)

与写作:

if(type==1)..;
Run Code Online (Sandbox Code Playgroud)

这使代码更难调试/理解

Kor*_*lis 9

我肯定会像Android团队一直在做的那样:在你的MySeekBarWidget班级中定义常量,就像这样.

所以在你的情况下,你定义:

class MySeekBarWidget... {
    public static final int TYPE_MONEY = 1;
    public static final int TYPE_PERCENT = 2;
    public static final int TYPE_TIME = 3;
...
Run Code Online (Sandbox Code Playgroud)

等等.在那之后,很容易引用那些:

type = array.getInt(R.styleable.MySeekBarWidget_type, TYPE_MONEY);
Run Code Online (Sandbox Code Playgroud)

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;
        }
    }
Run Code Online (Sandbox Code Playgroud)

然后,它可能是这样的:

type = array.getInt(R.styleable.MySeekBarWidget_type, Type.MONEY);
Run Code Online (Sandbox Code Playgroud)

从外面访问变得非常干净:

int outsideType = MySeekBarWidget.Type.MONEY;
Run Code Online (Sandbox Code Playgroud)

之后,您还可以通过执行以下操作来检查所处的类型:

if (type == Type.MONEY) {
    ...
} else if (type == Type.PERCENT) {
    ...
}
Run Code Online (Sandbox Code Playgroud)

或者甚至更好,为了避免重复,Enum可以在switch语句中使用s :

switch (type) {
    case Type.MONEY:
        ...
        break;
    case Type.PERCENT:
        ...
        break;
}
Run Code Online (Sandbox Code Playgroud)

这减少了重复次数type ==并提供了一种向条件添加新类型的简洁方法.

  • 您的解决方案不能正常工作.你在哪做`array.getInt(...,Type.Money)`是错误的.你需要明确地`Type.Money.getValue()` (3认同)