美文网首页
android注解

android注解

作者: 蒸汽飞船 | 来源:发表于2018-08-21 15:08 被阅读9次

    android注解:
    通过android.support.annotation库:

    1. NonNull不为空:
      成员变量、局部变量、参数、方法返回
    @NonNull    //控制结果
    public View xxx(String name, @NonNull Context context,//控制参数
      @NonNull AttributeSet attrs) {
      ...
    }
    
    1. CallSuper 注解:
    @CallSuper
    protected void onCreate(Bundle savedInstanceState) {
    }
    
    1. keep注解
      注解可以确保如果在构建时缩减代码,标注的元素不会移除。它一般会添加到通过反射访问的方法和类中,以阻止编译器将代码视为未使用。

    2. 资源注解:
      @StringRes
      @DrawableRes
      @DimenRes
      @ColorRes
      @AnyRes 任意类型R资源
      @InterpolatorRes 插值器资源

    3. 线程注解:
      @MainThread / @UiThread
      @WorkerThread
      @BinderThread
      @AnyThread

    4. 值约束注解:
      @IntRange (@IntRange(from=0,to=255) int alpha)
      @FloatRange
      @Size 注解可以检查集合或数组的大小,以及字符串的长度。

    • 最小大小(例如 @Size(min=2))
    • 最大大小(例如 @Size(max=2))
    • 确切大小(例如 @Size(2))
    • 表示大小必须为此倍数的数字(例如 @Size(multiple=2))
    1. 权限注解
      @RequiresPermission
    1. Typedef 注解
      使用 @IntDef 和 @StringDef 注解,以便能够创建整型和字符串集的枚举. flag属性标明是否可以位运算
    //先定义常量
    public static final int NAVIGATION_MODE_STANDARD = 0;
    public static final int NAVIGATION_MODE_LIST = 1;
    public static final int NAVIGATION_MODE_TABS = 2;
    
    
    @Retention(RetentionPolicy.SOURCE)
    @IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS})
    public @interface NavigationMode {}
    
    
    @NavigationMode  //指定返回结果
    public abstract int getNavigationMode();
    
    public abstract void setNavigationMode(@NavigationMode int mode);//指定参数
    

    //示例2带flag

    @IntDef(flag=true, value={
            DISPLAY_USE_LOGO,
            DISPLAY_SHOW_HOME,
            DISPLAY_HOME_AS_UP,
            DISPLAY_SHOW_TITLE,
            DISPLAY_SHOW_CUSTOM
    })
    @Retention(RetentionPolicy.SOURCE)
    public @interface DisplayOptions {}
    

    相关文章

      网友评论

          本文标题:android注解

          本文链接:https://www.haomeiwen.com/subject/bxhjiftx.html