安卓Support Anotation学习

本文详细介绍了Android Support库中提供的各类注解,包括Null注解、资源类型注解、类型定义注解等,并通过示例代码展示了它们的具体应用。

概述

今天来看看Android的support包中为我们提供的一套注解,
总共可分为以下几类注解

1.Null注解
2.资源类型注解
3.类型定义注解
4.线程注解
5.值范围注解
6.权限注解
7.重写函数注解
8.返回值注解
9.Keep注解


Null注解和资源类型注解

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        testNull(null);
        testBoolean(0);
        testColor(getColor(R.color.colorAccent));
        testDrawable(R.drawable.ic_launcher);
        testLayout(R.layout.activity_main);
        findId(R.id.bottom);
        testString(R.string.app_name);
        testStyle(R.style.AppTheme);
    }
    //参数不能为空,为null会给出警告
    public void testNull(@NonNull String content){

    }
    //返回值可以为空
    public @Nullable String getStr(){
        return null;
    }
    //标记整型值是布尔类型
    public void testBoolean(@BoolRes int flag){

    }
    //标记整型值是R.color类型
    public void testColor(@ColorRes int color) {

    }

    //标记参数类型为getResource().getColor类型
    public void testColor2(@ColorInt int color) {

    }
    //标记类型是R.drawable
    public void testDrawable(@DrawableRes int resId){

    }
    //只能设置R.layout的参数
    public void testLayout(@LayoutRes int layoutResId) {

    }
    //参数类型是R.id类型
    public void findId(@IdRes int id){

    }
    //参数类型是R.string
    public void testString(@StringRes int content){

    }
    //参数类型是R.style
    public void testStyle(@StyleRes int styleRedId){

    }

类型定义注解


public class ActionType {
    private static final int DEFAULT_TYPE = ActionType.ONE;
    private @Type int type;
    @Retention(RetentionPolicy.SOURCE)
    //定义可以接收的常量列表
    @IntDef({ONE, TWO, THREE})
    @interface Type {
    }

    static final int ONE = 1;
    static final int TWO = 2;
    static final int THREE = 3;

    @Type
    public int getType(){
        return DEFAULT_TYPE;
    }

    public void setType(@Type int type){
        this.type = type;
    }
    //================测试带标记的
    @Retention(RetentionPolicy.RUNTIME)
    //flag标记代表是否可以把常量作为一个标记,也就是是否可以设置多个,默认false
    @IntDef(flag = true,value = {ONE, TWO, THREE})
    @interface TYPE2 {
    }

    @TYPE2
    public int getType2(){
        return DEFAULT_TYPE;
    }

    public void setType2(@TYPE2 int type){

    }
使用如下
   //测试@intDef
        ActionType actionType = new ActionType();
        //设置类型
//        actionType.setType(0);有警告,必须为指定的类型
        actionType.setType(ActionType.ONE);
        //获取类型
        actionType.getType();//提示你获取返回值
//带标记的,可以设置多个
        actionType.setType2(ActionType.ONE|ActionType.TWO);

线程注解

  @Override
    protected void onStart() {
        super.onStart();
        SimpleTask simpleTask = new SimpleTask();
        simpleTask.execute();
    }

    static class SimpleTask extends AsyncTask<URL,Integer,Long>{

        @Override
        @MainThread//通常注解生命周期相关的函数
        protected void onPreExecute() {
            super.onPreExecute();
        }

        @Override
        @WorkerThread//后台线程
        protected Long doInBackground(URL[] objects) {
            return null;
        }

        @Override
        @MainThread
        protected void onProgressUpdate(Integer... values) {
            super.onProgressUpdate(values);
        }

        @Override
        @MainThread
        protected void onPostExecute(Long o) {
            super.onPostExecute(o);
        }
    }
@UiThread通常用来注解和View相关的,比如View就用了
@UiThread
public class View implements Drawable.Callback, KeyEvent.Callback,
        AccessibilityEventSource {
@BinderThread标记用在Binder线程中


值范围注解

 通常用于数组,集合,字符串

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
//        setAlpha(260);值超出范围会有红色警告
        setAlpha(255);
        setText("aaaaaa");
        setText2("222");
    }
    //传入的参数只能在0-255
    public void setAlpha(@IntRange(from = 0,to = 255) int alpha){

    }
    //传入的字符串长度在4-6
    public void setText(@Size(min = 4,max = 6) String text){

    }
    //传入的字符串长度必须是3
    public void setText2(@Size(3) String text){

    }

    //传入的字符串长度必须是2的倍数
    public void setText3(@Size(multiple = 2) String text){

    }

权限注解

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        callPhone();//如果没有添加权限,会提示对应的权限
        store();
    }
    @RequiresPermission(Manifest.permission.CALL_PHONE)
    public void callPhone(){

    }
    //需要所有的权限
    @RequiresPermission(allOf = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
    Manifest.permission.READ_EXTERNAL_STORAGE})
    public void store(){
        
    }
    //需要任意一个权限
    @RequiresPermission(anyOf = {Manifest.permission.WRITE_EXTERNAL_STORAGE,
            Manifest.permission.READ_EXTERNAL_STORAGE})
    public void store2(){

    }


返回值注解

  @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        checkResule();
    }
    //注解的函数如果返回值没有接收,会有黄色警告
    @CheckResult
    public String checkResule(){
        return "";
    }
}


Keep注解

用来标记在Proguard过程中不需要混淆的地方






<think>好的,用户再次遇到了同样的错误:“anotation argument must be a compile-time constant”。之前已经回答过这个问题,现在需要再次处理。可能用户之前的解决方案没有完全解决问题,或者他们在不同的场景下遇到了同样的错误。 首先,我需要确认用户的当前情况。用户可能是在使用Java或Kotlin时,在注解中使用了非编译时常量的参数。这次可能需要更详细的解释或者不同的示例来帮助他们理解问题。 检查之前的回答,已经涵盖了常见原因和修复示例,包括Java和Kotlin的情况。但用户再次提问,可能意味着需要更深入的解释,或者他们遇到了特殊情况,比如使用了不同的框架或注解处理器。 可能的遗漏点包括:其他允许作为注解参数的类型,例如枚举、Class对象,或者数组类型的处理。另外,用户可能在使用自定义注解时,参数的默认值是否需要编译时常量,或者如何处理复杂的常量表达式。 此外,用户可能对编译时常量的定义不够清楚,比如静态常量是否必须是public,或者在接口中的常量是否适用。需要明确这些细节。 还需要考虑不同的构建工具配置问题,例如Maven与Gradle在处理注解处理器时的差异,或者是否遗漏了某些编译时依赖,导致注解处理器无法生成代码,从而使得注解参数无法解析为常量。 可能的解决方案包括检查注解处理器的配置,确保所有依赖正确,并且生成的代码被正确包含在编译过程中。此外,提醒用户检查IDE的缓存问题,有时候旧的编译文件可能导致此类错误。 最后,确保相关问题部分涵盖用户可能遇到的其他相关疑问,例如不同数据类型的使用、动态参数的处理等。</think>该错误表明注解参数未使用编译时常量。Java/Kotlin要求注解属性值必须是编译时可确定的常量,否则会触发此错误。 **根本原因**: 1. 参数值未使用 `static final`(Java)或 `const val`(Kotlin) 2. 参数类型不符合规范(仅支持基本类型、String、Class、枚举、数组等) 3. 参数值依赖运行时计算(如方法调用或动态表达式) **修复方案**: ```java // Java示例 public class Constants { public static final String API_ENDPOINT = "user/profile"; // 正确 public static String getTimeout() { return "30s"; } // 运行时常量(错误) } // 错误用法 @Request(timeout = Constants.getTimeout()) // 正确用法 @Request(endpoint = Constants.API_ENDPOINT) ``` ```kotlin // Kotlin特殊要求 class Config { companion object { const val MAX_RETRY = 3 // 正确 val DYNAMIC_VAL = computeValue() // 运行时值(错误) } } // 正确用法 @Retry(max = Config.MAX_RETRY) ``` **进阶验证**: 1. 检查注解处理器是否生成代码(查看`build/generated/source/apt/`目录) 2. 验证`javac -parameters`等编译参数是否影响常量折叠 3. 使用`@IntDef`/`@StringDef`时需确保常量被`public static final`修饰
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值