Some resource annotations in Android, let the compiler help you check the code

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.