Some resource annotations in Android, let the compiler help you check the code
When writing is convenient, you can use annotations to declare some parameters to clearly indicate the type of the parameter, making the code more secure. We see that annotations are used extensively in the Android source code. I have compiled some notes as follows:
@NonNull tell the compiler that this parameter is non-null and the compiler will check it for you.
Example:
@NonNull
public static Snackbar make (@NonNull view view, @StringRes int resId, @Duration int Duration) {
return make (view, View.getresources (). GetText (ResId), duration);
}
@Nullable the declaration parameter is likely to be empty, the compiler will help you make a check
Example:
@Override
protected void OnCreate (@Nullable Bundle savedinstancestate) {}
@IdRes declare that the int parameter is an ID and the compiler will check it for you.
Public View Findviewbyid (@IdRes int id) {
Return Getdelegate (). Findviewbyid (ID);
}
The @StringRes declares that the int parameter is a string resource that the compiler will check for you
Example:
@NonNull
Public Snackbar setaction (@StringRes int resId, View.onclicklistener listener) {
Return Setaction (Mcontext.gettext (resId), listener);
}
@StyleRes the declaration parameter is a style type, the compiler will check for you
@Override
public void SetTheme (@StyleRes final int resid) {}
@LayoutRes the declaration parameter is a layout type, the compiler will help you to make a check
Example:
public void Setcontentview (@LayoutRes int layoutresid)
There's a lot like that, you know what you do with your name.
@DimenRes @DrawableRes @RawRes @ColorRes @XmlRes
@BoolRes @IntegerRes @StyleableRes
Another:
@Keep declares that this method is not confused
Some resource annotations in Android, let the compiler help you check the code