java - 参数列表上的注解可以同时有多个吗?
PHPz
PHPz 2017-04-18 10:53:55
0
1
694

我有一个方法,在参数列表上有一个参数(接受一个颜色类型),数据类型是int型

但是我的方法内部,这个int型根据传入的一个boolean类型做了判断,
所以它

 * 既可以是`@ColorRes` (颜色代码的资源ID,int类型)
 * 也可以是`@ColorInt`  (颜色代码在Color类中的成员变量,int类型)
 

但是方法参数上又不能写(@ColorRes|@ColorInt) 同样的参数列表也不能用方法重载来搞定。
所以就想问一下有没有办法可以让两个注解同时生效的?

PHPz
PHPz

学习是最好的投资!

reply all(1)
Ty80

Multi-annotation code is as follows:

public void setColor(@ColorInt @ColorRes int color){
    // TODO 
}

There is a problem with writing this way. For IDEs, this is ambiguous. If you want a Color value and a Color resource reference, will there be such an Int value?
This type of annotation itself is only for IDE assistance. , that is, problems are discovered during compilation. You can disable this type of Lint by opening Inspections, as shown below:

Why not refer to the Android source code instead of defining a method that makes the caller crazy? The source code is as follows:

public void setBackgroundColor(@ColorInt int color) {
    ...
}

public void setBackgroundResource(@DrawableRes int resid) {
    ...
}
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template