Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android @IntDef a Local Variable Example

https://developer.android.com/reference/android/support/annotation/IntDef.html

I'm looking for an example how to define a local variable.

I see how to create a function return, a member variable, a function parameter, but no example to assign a local variable using the Android annotated element @IntDef.

Thanks

UPDATE

Here's one example of what is not working. Does this have something to do with where to put the @Retention? I don't understand how the compiler knows what to apply the Retention Policy to. Is this a global setting?

int foobar() {
        @IntDef({
                ItemType.TYPE1,
                ItemType.TYPE2
        })
        @Retention(RetentionPolicy.SOURCE)
        @interface ItemType {
            int TYPE1 = 0;
            int TYPE2 = 1;
        }


        @ItemType int type = TYPE1;
...
}

Another example that doesn't work for me:

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

    final @View.MeasureSpec.MeasureSpecMode int heightMode = MeasureSpec.getMode(heightMeasureSpec);

...
like image 537
Mitch Avatar asked Jun 15 '26 04:06

Mitch


1 Answers

@IntDef({
  ItemType.TYPE1,
  ItemType.TYPE2
})
public @interface ItemType {
  int TYPE1 = 0;
  int TYPE2 = 1;
}

// use it in global variable like
@ItemType
private int type;

// use it in local variable like
public void add(@ItemType int type){

}
like image 170
Linh Avatar answered Jun 17 '26 19:06

Linh



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!