Skip to content
文章摘要

url

发现一个很好的总结http://www.flysnow.org/2015/08/13/android-tech-docs-support-annotations.html?utm_source=tuicool&utm_medium=referral

AnimatorRes Denotes that an integer parameter, field or method return value is expected to be an animator resource reference (e.g. fade_in).

代表这个整形的参数,属性或者方法的值(返回值)是一个动画(animator)资源的引用 例如:android.R.animator.fade_in

AnimRes Denotes that an integer parameter, field or method return value is expected to be an anim resource reference  (e.g. fade_in).

代表这个整形的参数,属性或者方法的值(返回值)是一个动画(anim)资源的引用 例如:android.R.anim.fade_in

AnyRes Denotes that an integer parameter, field or method return value is expected to be a resource reference of any type. If the specific type is known, use one of the more specific annotations instead, such as StringRes or DrawableRes.

代表这个整形的参数,属性或者方法的值(返回值)是一个资源引用的任何类型。如果这个指定的资源是已知的,使用一个更确定的注解代替 例如 StringRes 注解或者 DrawableRes 注解。

ArrayRes Denotes that an integer parameter, field or method return value is expected to be an array resource reference (e.g. phoneTypes).

代表这个整形的参数,属性或者方法的值(返回值)是一个数组资源的引用 例如:android.R.arraay.phoneTypes

AttrRes Denotes that an integer parameter, field or method return value is expected to be an attribute reference (e.g. action).

代表这个整形的参数,属性或者方法的值(返回值)是一个属性资源的引用 例如:android.R.attr.action

BinderThread Denotes that the annotated method should only be called on the binder thread. If the annotated element is a class, then all methods in the class should be called on the binder thread. Example: @BinderThread  public BeamShareData createBeamShareData() { ...  }

代表这个注解的方法只应该在绑定的线程调用。如果注解的在类上,那么这个类的所有方法都应该在绑定的线程上调用。

BoolRes Denotes that an integer parameter, field or method return value is expected to be a boolean resource reference.

代表这个整形的参数,属性或者方法的值(返回值)是一个布尔 ( boolean ) 资源的引用

CallSuper Denotes that any overriding methods should invoke this method as well. Example: CallSuper  public abstract void onFocusLost();

代表任何 overriding 方法都应该调用这个方法

CheckResult Denotes that the annotated method returns a result that it typically is an error to ignore. This is usually used for methods that have no side effect, so calling it without actually looking at the result usually means the developer has misunderstood what the method does. Example: ublic @CheckResult String trim(String s) { return s.trim(); } ...  s.trim(); // this is probably an error  s = s.trim(); // ok }

代表方法的返回值通常被忽略从而导致错误的调用。通常使用在没有副作用的方法上,所以在调用方法的时候忽略方法的返回值意味着调用者不理解这个方法的作用。 例如 trim 方法 s.trim();//错误的调用,没用注意到方法的返回值,错误的理解为调用此方法就会将 s 字符串去空格。正确的使用方法应该是:s = s.trim();

ColorInt Denotes that the annotated element represents a packed color int, AARRGGBB If applied to an int array, every element in the array represents a color integer. Example: ublic abstract void setTextColor(@ColorInt int color);

被注解的元素是一个包装之后的颜色整型值 ( 格式:AARRGGBB ),如果作用在整型数组上说明数组中的每一个值都代表一个颜色值

ColorRes Denotes that an integer parameter, field or method return value is expected to be a color resource reference (e.g. black).

代表这个整型的参数,属性或者方法的值(返回值)是一个颜色的资源值。例如:android.R.color.black

DimenRes Denotes that an integer parameter, field or method return value is expected to be a dimension resource reference (e.g. app_icon_size).

代表这个整型的参数,属性或者方法的值(返回值)是一个尺寸的资源值。例如:android.R.dimen.app_icon_size

DrawableRes Denotes that an integer parameter, field or method return value is expected to be a drawable resource reference (e.g. alertDialogIcon).

代表这个整型的参数,属性或者方法的值(返回值)是一个 drawable 的资源值。例如:android.R.drawable.alertDialogIcon

FloatRange Denotes that the annotated element should be a float or double in the given rangeExample: FloatRange(from=0.0,to=1.0)  public float getAlpha() {      ...   }

代表注解的元素应该是在给定范围内的一个浮点数或者双精度值

FractionRes Denotes that an integer parameter, field or method return value is expected to be a fraction resource reference.

代表这个整型的参数,属性或者方法的值(返回值)是一个 fraction(分数)的资源值。

IdRes Denotes that an integer parameter, field or method return value is expected to be an id resource reference (e.g. copy).

代表这个整型的参数,属性或者方法的值(返回值)是一个 id 的资源值。例如:android.R.id.copy

IntDef Denotes that the annotated element of integer type, represents a logical type and that its value should be one of the explicitly named constants. If the IntDef#flag() attribute is set to true, multiple constants can be combined. Example: @Retention(SOURCE) //源码级别的注解,不被记录在 class 文件中 @IntDef({NAVIGATION_MODE_STANDARD,NAVIGATION_MODE_LIST,NAVIGATION_MODE_TABS}) public @interface NavigationMode {}//声明 navigationMode 注解 public static final int NAVIGATION_MODE_STANDARD = 0; public static final int NAVIGATION_MODE_LIST = 1; public static final int NAVIGATION_MODE_TABS = 2;  ... //参数 mode 的值只能是 intDef 中声明的值 public abstract void setNavigationMode(@NavigationMode int mode);

//函数的返回值只能是 intDef 中声明的值 @NavigationMode public abstract int getNavigationMode();

For a flag, set the flag attribute: @IntDef(     flag = true//此时注解的值可以是组合的形式 value ={NAVIGATION_MODE_STANDARD,NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})

用于生成一个注解 ( 用于限定输入或者返回的值 ) ,被这个注解标识的整型必须是在 IntDef 中声明的值。如果想要被标注的值可以以组合的形式输入 ( 用 &,| 等符号连接) 则需要将 IntDef 中 flag 的值设置为 true。

IntegerRes Denotes that an integer parameter, field or method return value is expected to be an integer resource reference (e.g. config_shortAnimTime).

代表这个整型的参数,属性或者方法的值(返回值)是一个 integer 的资源值。例如:android.R.integer.config_shortAnimTime ( 一个短动画的持续时间,毫秒 )

InterpolatorRes Denotes that an integer parameter, field or method return value is expected to be an interpolator resource reference (e.g. [cycle](https://developer.android.com/reference/android/R.interpolator.html “ android.R.interpolator.cycle")).

代表这个整型的参数,属性或者方法的值(返回值)是一个插值器的资源值。例如:android.R.interpolator.cycle

IntRange Denotes that the annotated element should be an int or long in the given range Example: IntRange(from=0,to=255)  public int getAlpha() {     ...   }

代表注解的元素是在给定范围内的一个 int 或者 long 值

Keep Denotes that the annotated element should not be removed when the code is minified at build time. This is typically used on methods and classes that are accessed only via reflection so a compiler may think that the code is unused. Example: Keep  public void foo() {     ...   }

保证这个方法或者类不被混淆

LayoutRes Denotes that an integer parameter, field or method return value is expected to be a layout resource reference (e.g. list_content).

代表这个整型的参数,属性或者方法的值(返回值)是一个布局的资源值。例如:android.R.layout.list_content

MainThread Denotes that the annotated method should only be called on the main thread. If the annotated element is a class, then all methods in the class should be called on the main thread. Example: MainThread  public void deliverResult(D data) { ...  }

被标注的方法只能运行在主线程上,如果被标注的元素是类那么这个类的所有方法都应该运行在主线程

MenuRes Denotes that an integer parameter, field or method return value is expected to be a menu resource reference.

代表这个整型的参数,属性或者方法的值(返回值)是一个 menu 的资源值

NonNull Denotes that a parameter, field or method return value can never be null. his is a marker annotation and it has no specific attributes.

代表这个参数,属性或者方法的值不能为空。知识一个标记注解而且这个注解没有具体的属性

Nullable Denotes that a parameter, field or method return value can be null.

When decorating a method call parameter, this denotes that the parameter can legitimately be null and the method will gracefully deal with it. Typically used on optional parameters.

When decorating a method, this denotes the method might legitimately return null.

This is a marker annotation and it has no specific attributes.

代表这个参数,属性或者方法的值可以为空

当被用来标注方法的参数时,表示这个参数可以合理的为空并且这个方法会优雅的处理。通常被用于可选参数上

当被用来标注方法时,表示这个方法可能会返回空

这是一个标记注解,他没有具体的属性

PluralsRes Denotes that an integer parameter, field or method return value is expected to be a plurals resource reference.

代表这个整型的参数,属性或者方法的值(返回值)是一个 plurals(复数)的资源值 --表示木有遇到过--

RawRes Denotes that an integer parameter, field or method return value is expected to be a raw resource reference.

代表这个整型的参数,属性或者方法的值(返回值)是一个 raw 的资源值

Size Denotes that the annotated element should have a given size or length. Note that "-1" means "unset". Typically used with a parameter or return value of type array or collection. Example: ublic void getLocationInWindow(@Size(2) int[] location) {      ...   }

被标注的元素的长度或大小必须是指定的值,其中-1 表示未设置。通常用在(类型是数组或集合的参数)or(返回值是数组或者集合的方法)中

StringDef Denotes that the annotated String element, represents a logical type and that its value should be one of the explicitly named constants. Example: @Retention(SOURCE)//注解只存在源码中,不写入 class 文件 StringDef({POWER_SERVICE, WINDOW_SERVICE, LAYOUT_INFLATER_SERVICE  })  public @interface ServiceName {}//声明一个注解 public static final String POWER_SERVICE = "power"; public static final String WINDOW_SERVICE = "window"; public static final String LAYOUT_INFLATER_SERVICE = "layout_inflater";  ...  //参数 name 的值只能是 StringDef 中的值 ublic abstract Object getSystemService(@ServiceName String name);

用于生成一个注解 ( 用于限定输入或者返回的值 ) ,被这个注解标识的字符串必须是在 StringDef 中声明的值。如果想要被标注的值可以以组合的形式输入 ( 用 &,| 等符号连接) 则需要将 StringDef 中 flag 的值设置为 true。

StringRes Denotes that an integer parameter, field or method return value is expected to be a String resource reference (e.g. ok).

代表这个整型的参数,属性或者方法的值(返回值)是一个 String 的资源值, 例如:android.R.string.ok

StyleableRes Denotes that an integer parameter, field or method return value is expected to be a styleable resource reference (e.g. TextView_text).

代表这个整型的参数,属性或者方法的值(返回值)是一个 styleable 的资源值, 例如:android.R.styleable.TextView_text

StyleRes Denotes that an integer parameter, field or method return value is expected to be a style resource reference (e.g. TextAppearance).

代表这个整型的参数,属性或者方法的值(返回值)是一个 style 的资源值, 例如:android.R.style.TextAppearance

TransitionRes Denotes that an integer parameter, field or method return value is expected to be a transition resource reference.

代表这个整型的参数,属性或者方法的值(返回值)是一个 transition 的资源值

UiThread Denotes that the annotated method or constructor should only be called on the UI thread. If the annotated element is a class, then all methods in the class should be called on the UI thread. Example: UiThread  public abstract void setText(@NonNull String text) { ...  }

被注解的方法或者构造函数应该在 ui 线程被调用,如果标注在类上那么这个类的所有方法都应当运行在 ui 线程上

VisibleForTesting Denotes that the class, method or field has its visibility relaxed, so that it is more widely visible than otherwise necessary to make code testable.

你可以把这个注解标注到类、方法或者字段上,以便你在测试的时候可以使用他们。

WorkerThread Denotes that the annotated method should only be called on a worker thread. If the annotated element is a class, then all methods in the class should be called on a worker thread. Example: (@WorkerThread protected abstract FilterResults performFiltering(CharSequence constraint);

被注解的方法只能在工作线程被调用,如果标注在类上那么这个类的所有方法都应当只能在工作线程被调用

XmlRes Denotes that an integer parameter, field or method return value is expected to be an XML resource reference.

代表这个整型的参数,属性或者方法的值(返回值)是一个 xml 的资源值