安卓的注解支持

url:https://developer.android.com/reference/android/support/annotation/package-summary.html

只是官网的搬运工,发现一个很好的总结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:
public @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:
public 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.
This 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:
public 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中的值
public 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的资源值

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
  • 序言:七十年代末,一起剥皮案震惊了整个滨河市,随后出现的几起案子,更是在滨河造成了极大的恐慌,老刑警刘岩,带你破解...
    沈念sama阅读 200,176评论 5 469
  • 序言:滨河连续发生了三起死亡事件,死亡现场离奇诡异,居然都是意外死亡,警方通过查阅死者的电脑和手机,发现死者居然都...
    沈念sama阅读 84,190评论 2 377
  • 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
    开封第一讲书人阅读 147,232评论 0 332
  • 文/不坏的土叔 我叫张陵,是天一观的道长。 经常有香客问我,道长,这世上最难降的妖魔是什么? 我笑而不...
    开封第一讲书人阅读 53,953评论 1 272
  • 正文 为了忘掉前任,我火速办了婚礼,结果婚礼上,老公的妹妹穿的比我还像新娘。我一直安慰自己,他们只是感情好,可当我...
    茶点故事阅读 62,879评论 5 360
  • 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
    开封第一讲书人阅读 48,177评论 1 277
  • 那天,我揣着相机与录音,去河边找鬼。 笑死,一个胖子当着我的面吹牛,可吹牛的内容都是我干的。 我是一名探鬼主播,决...
    沈念sama阅读 37,626评论 3 390
  • 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
    开封第一讲书人阅读 36,295评论 0 254
  • 序言:老挝万荣一对情侣失踪,失踪者是张志新(化名)和其女友刘颖,没想到半个月后,有当地人在树林里发现了一具尸体,经...
    沈念sama阅读 40,436评论 1 294
  • 正文 独居荒郊野岭守林人离奇死亡,尸身上长有42处带血的脓包…… 初始之章·张勋 以下内容为张勋视角 年9月15日...
    茶点故事阅读 35,365评论 2 317
  • 正文 我和宋清朗相恋三年,在试婚纱的时候发现自己被绿了。 大学时的朋友给我发了我未婚夫和他白月光在一起吃饭的照片。...
    茶点故事阅读 37,414评论 1 329
  • 序言:一个原本活蹦乱跳的男人离奇死亡,死状恐怖,灵堂内的尸体忽然破棺而出,到底是诈尸还是另有隐情,我是刑警宁泽,带...
    沈念sama阅读 33,096评论 3 315
  • 正文 年R本政府宣布,位于F岛的核电站,受9级特大地震影响,放射性物质发生泄漏。R本人自食恶果不足惜,却给世界环境...
    茶点故事阅读 38,685评论 3 303
  • 文/蒙蒙 一、第九天 我趴在偏房一处隐蔽的房顶上张望。 院中可真热闹,春花似锦、人声如沸。这庄子的主人今日做“春日...
    开封第一讲书人阅读 29,771评论 0 19
  • 文/苍兰香墨 我抬头看了看天上的太阳。三九已至,却和暖如春,着一层夹袄步出监牢的瞬间,已是汗流浃背。 一阵脚步声响...
    开封第一讲书人阅读 30,987评论 1 255
  • 我被黑心中介骗来泰国打工, 没想到刚下飞机就差点儿被人妖公主榨干…… 1. 我叫王不留,地道东北人。 一个月前我还...
    沈念sama阅读 42,438评论 2 346
  • 正文 我出身青楼,却偏偏与公主长得像,于是被迫代替她去往敌国和亲。 传闻我的和亲对象是个残疾皇子,可洞房花烛夜当晚...
    茶点故事阅读 42,032评论 2 341

推荐阅读更多精彩内容

  • 序言 作为一个已经带过10多款智能手表、手环的数码尝鲜者,对于一款完美的穿戴设备的渴望是常人无法想象的! 作为一个...
    此处空无一人阅读 588评论 0 1
  • 刚刚回看了一下《最忆是杭州》 最后烟花绽放、友谊地久天长的音乐响起 各国政要纷纷拿下手机拍照 这个时候所有杭州人民...
    创享家金少阅读 472评论 0 0
  • 窗外风雪洗礼了一个冬天的梧桐树,枝干依然干枯,树枝上不知哪里刮来的塑料袋在枝头迎风飘荡,加上最近几年雾霾的影响...
    小寨姑娘阅读 79评论 0 0
  • 感恩出门在外依然坚持晨跑,为我们的健康种下好种子,一天不拉下,感恩老公的陪伴。 感恩美丽的珠江奉献给我们的早晚美景...
    rainlove2011阅读 195评论 0 0