skip to content
Posts · November 2021

Android Annotation


Android注解收集

官方说明

@Nullable @NonNull

@StringRes @IdRes @DrawableRes @ColorRes @DimenRes @InterpolatorRes @LayoutRes @AnyRes

@ColorInt

@StringDef @IntDef

@MainThread @UiThread @WorkerThread @BinderThread @AnyThread

@IntRange @FloatRange @Size

@TargeApi @RequiresPermission @RequiresPermission.Read @RequiresPermission.Write

@Throws

@SdkConstant

@CheckResult @CallSuper

@Keep

@VisibleForTesting

@RestrictTo

示例

import android.support.annotation.IntDef
//...
// Define the list of accepted constants and declare the NavigationMode annotation
@Retention(AnnotationRetention.SOURCE)
@IntDef(NAVIGATION_MODE_STANDARD, NAVIGATION_MODE_LIST, NAVIGATION_MODE_TABS)
annotation class NavigationMode
// Declare the constants
const val NAVIGATION_MODE_STANDARD = 0
const val NAVIGATION_MODE_LIST = 1
const val NAVIGATION_MODE_TABS = 2
abstract class ActionBar {
    // Decorate the target methods with the annotation
    // Attach the annotation
    @get:NavigationMode
    @setparam:NavigationMode
    abstract var navigationMode: Int
}
@SdkConstant(SdkConstantType.ACTIVITY_INTENT_ACTION)
@RequiresPermission(Manifest.permission.CALL_PHONE)
const val ACTION_CALL = "android.intent.action.CALL"
@CheckResult(suggest = "#enforcePermission(String,int,int,String)")
abstract fun checkPermission(permission: String, pid: Int, uid: Int): Int
// 对public方法或属性进行注解,限制访问
@RestrictTo(RestrictTo.Scope.SUBCLASSES)
@RestrictTo(RestrictTo.Scope.GROUP_ID)
@RestrictTo(RestrictTo.Scope.TESTS)