Home  >  Q&A  >  body text

java - 参数列表上的注解可以同时有多个吗?

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

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

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

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

PHPzPHPz2743 days ago663

reply all(1)I'll reply

  • PHP中文网

    PHP中文网2017-04-18 10:55:55

    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) {
        ...
    }
    

    reply
    0
  • Cancelreply