Android Annotation
dependency:
dependencies {
// If you stay on Support Library
implementation 'com.android.support:support-annotations:28.0.0' // If you use AndroidX
implementation 'androidx.annotation:annotation:1.0.1'
}
限定參數型別,類似防呆的做法,像是
private void showToast(Context context, @StringRes strMsg)
{
Toast.makeText(context, strMsg, Toast.LENGTH_SHORT).show();
}
或是Java與kotlin協作的時候可以用到non-null的判斷
private void showMsg(@NonNull SimpleObj obj)
{
...
}
數字範圍判斷:IntRange/FloatRange
public void count(@IntRange(from = 1, to = 100) int times)
{
}
IdRes/LayoutRes/DrawableRes/...[Any]Res
要求某權限:ex.@RequirePermission(Manifest.permission.ACCESS_FINE_LOCATION)
Last updated
Was this helpful?