Android Support Annotations :安卓註解快速上手

 咱們都知道,安卓資源文件都是int類型的ID來保存其引用,經過註解類型,能夠讓咱們在寫代碼的時候,及時發現參數類型的錯誤,避免潛在的BUG,以下:android

咱們經過@LayoutRes指定了參數必需要是R.layout.xxx格式的數據,傳數字IDE就會提示咱們錯誤gradle

 

經過gradle,把註解類型引入到項目中 code

compile 'com.android.support:support-annotations:23.1.1'

 

安卓原生給咱們提供了一系列註解類,支持咱們的開發blog

註解類所在包位置:安卓SDK路徑\extras\android\m2repository\com\android\support\support-annotations資源

 

咱們隨便找個23.1.1文件夾,找到裏面的support-annotations-23.1.1-sources.jar,經過JD-GUI查看開發

經過裏面Res結尾的類,咱們就能夠限定安卓不一樣類型的資源ID了get

裏面其餘類咱們也能夠看看,好比NonNull、Nullable、限定範圍FloatRange的也頗有意思,以下:it

 

自定義註解

定義io

public static final int RESOLUTION_540P = 1;
public static final int RESOLUTION_720P = 2;
public static final int RESOLUTION_1080P = 3;

@IntDef({RESOLUTION_540P, RESOLUTION_720P, RESOLUTION_1080P})
@Retention(RetentionPolicy.SOURCE)
public @interface ResolutionType {

}
調用
private @ResolutionType int mResolutionType;
相關文章
相關標籤/搜索