使用Android Support Annotations优化你的代码
来源:互联网 发布:孔孝真 赵寅成 知乎 编辑:程序博客网 时间:2024/06/05 14:56
Android Support Annotations提供的注解允许你像lint检查那样提供一些提示信息在审查代码上,这可以帮助你解决一些难以发现的代码问题。
添加依赖
- 选择File > Project Structure
- 点击Dependencies标签
- 点击“+”按钮然后选择Library dependency那项
- 在弹出的选择框选择support-annotations然后点击ok
用上面的方法添加的依赖一般都是最新的,然后在build.gradle就会多出一行依赖。
dependencies { compile 'com.android.support:support-annotations:23.3.0'}
Nullness 注解
添加@Nullable和@NonNull注解去检验一个给定的变量,参数或者返回的值是否有效。
@Nullable 表示一个参数,变量,或方法返回值可以为null
@NonNull 表示一个参数,变量,或方法返回值不能为null
例如添加了@NonNull这个注解表示context和attrs这两个参数不能为空
import android.support.annotation.NonNull;... /** Add support for inflating the <fragment> tag. */ @NonNull @Override public View onCreateView(String name, @NonNull Context context, @NonNull AttributeSet attrs) { ... }...
Resource 注解
对Android引用的资源类型验证,可以用来区分像Drawables和 R.string这种同是integer的类型资源。
添加@StringRes注解检验resId是否为string的资源id
import android.support.annotation.StringRes;... public abstract void setTitle(@StringRes int resId); ...
同类型的注解还有 @DrawableRes, @DimenRes, @ColorRes, 和@InterpolatorRes等等
Thread 注解
如果一个方法需要在特定类型的线程中调用,就可以添加这些注解帮助代码审查。
@UiThread
@MainThread
@WorkerThread
@BinderThread
注意: @MainThread 和 @UiThread 是可以互换的,所以被注解方法只允许从这2个注解声明的线程中调用。
Value Constraint 注解
使用@IntRange, @FloatRange, 和 @Size 注解检对传递的参数进行验证。
- @IntRange 指定一个int的值必须在一个规定范围内
例如取一个alpha值在0-255之间
public void setAlpha(@IntRange(from=0,to=255) int alpha) { … }
- @FloatRange 指定一个float的值必须在一个规定范围内
例如取一个alpha值在0.0-1.0之间
public void setAlpha(@FloatRange(from=0.0, to=1.0) float alpha) {...}
- @Size 表示被注解的元素必须有一个给定的数量或长度
@Size 注解检查集合或数组的数量,以及String的长度。例如一个集合必须要有一个存在的值就使用@Size(min=1)注解去检查这个集合的是否合法。又比如使用 @Size(2) 注解表示这个数组必须要包含两个存在的值。
例子:表示location这个数组必须要有一个值
int[] location = new int[3];button.getLocationOnScreen(@Size(min=1) location);
Permission 注解
使用@RequiresPermission注解用来检验一个方法的调用者是否已经拥有此权限。对只检查单个权限是否为有效权限,使用anyOf属性,对检查一组权限使用allOf属性。
@RequiresPermission(Manifest.permission.SET_WALLPAPER)public abstract void setWallpaper(Bitmap bitmap) throws IOException;
@RequiresPermission(allOf = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE})public static final void copyFile(String dest, String source) { ...}
CheckResults 注解
使用@CheckResults注解检验方法的结果或返回值是否有使用。
@CheckResult(suggest="#enforcePermission(String,int,int,String)")public abstract int checkPermission(@NonNull String permission, int pid, int uid);
CallSuper 注解
使用@CallSuper注解检验方法是否调用super实现了父类的方法。
@CallSuperprotected void onCreate(Bundle savedInstanceState) {}
Enumerated 注解
使用@IntDef 和 @StringDef 注解可以使用注解方式代替枚举实现整型或字符串的列举类型,以检验其他类型的代码引用。
import android.support.annotation.IntDef;...public abstract class ActionBar { ... //Define the list of accepted constants @IntDef({NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS}) //Tell the compiler not to store annotation data in the .class file @Retention(RetentionPolicy.SOURCE) //Declare the NavigationMode annotation public @interface NavigationMode {} //Declare the constants public static final int NAVIGATION_MODE_STANDARD = 0; public static final int NAVIGATION_MODE_LIST = 1; public static final int NAVIGATION_MODE_TABS = 2; //Decorate the target methods with the annotation @NavigationMode public abstract int getNavigationMode(); //Attach the annotation public abstract void setNavigationMode(@NavigationMode int mode);
如果模型参数不是定义好的常数之一,那么在你构建这段代码时就会生成一段警告。
如果一个参数或返回值引用一个有效的模式,你也可以定义带标志的注解。
import android.support.annotation.IntDef;...@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 Support Annotations优化你的代码
- 使用Android Support Annotations优化你的代码
- 使用Android Support Annotations优化代码
- Android注解使用之使用Support Annotations注解优化代码
- Android注解使用之使用Support Annotations注解优化代码
- Android代码优化Android Support Annotations
- Android Support Annotations 使用详解
- Android Support Annotations 使用详解
- Android中注解(Support Annotations)的使用
- Android support-annotations ColorRes 冲突的解决办法
- 深入浅出Android Support Annotations
- 深入浅出Android Support Annotations
- 深入浅出Android Support Annotations
- 深入浅出Android Support Annotations
- 深入浅出Android Support Annotations
- 深入浅出Android Support Annotations
- 深入浅出Android Support Annotations
- 【注解】android.support.annotations
- json 学习
- C语言中运算符的优先级问题
- 2016-8-15 %f和%lf的区别
- game design原理系列学习笔记(九)
- Android工程师成长之路
- 使用Android Support Annotations优化你的代码
- 高斯消元模版
- STM32F103C8开发USART通信(二)
- 常用的正则表达式
- JZOJ4696.第四次忍者大战
- 【连载】研究EasyUI系统——Form组件
- Android WebView使用(一)
- centos7+php+mysql5.6环境搭建
- 正则表达式验证是否是手机号