"Android" Android support Annotations

Source: Internet
Author: User

Android Support Annotations

View Original

In support Library 19.1 and later versions, the Android tools team introduced several cool annotation types that are easy for developers to use in the project, and the support library itself uses these annotations.

The code for this article is done using Android Studio. First, add annotation support:

compile ‘com.android.support:support-annotations:22.1.1‘

There are three types of annotations:

    1. Nonnull & Nullable
    2. Resource ID
    3. Intdef & Stringdef
nullness annotations

@NonNull used to decorate a parameter that cannot be null. In the following code example, we have a name variable with a value of NULL, which is passed as a parameter to the SayHello method, and the method requires that the parameter be a non-null string type:

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        String name = null;        sayHello(name);    }    void sayHello(@NonNull String name) {        Toast.makeText(this, "Hello " + s, Toast.LENGTH_LONG).show();    }}

Because the parameter name in the code is @NonNull annotated, Android studio warns us of a problem with this place in the form of a warning:

If we assign a value to name, for example

String name = “Our Lord Duarte”

Then the warning will disappear.

"Note: This I tried, in Android studio, even without annotations, there will be a hint, Android studio is so smart." 】

@Nullable the parameter used to modify the method, or the return value may be null. Examples are as follows:

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        Toast.makeText(this, getName().toLowerCase(), Toast.LENGTH_LONG).show();    }    @Nullable    String getName() {        return "";    }}

Because the return value of the GetName method uses @Nullable adornments, Android studio prompts

Method invocation "getName().toLowerCase()" may produce "java.lang.NollPointerException"
Resource annotations

Resource type annotations can help us to use resource IDs accurately, for example, to avoid the wrong use of Dimenid where we ask for Colorid.
In the following code, our SayHello method expects to accept a resource ID of a string type and use the @stringres annotation adornment:

public class MainActivity extends ActionBarActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {        super.onCreate(savedInstanceState);        setContentView(R.layout.activity_main);        sayHello(R.style.AppTheme);    }    void sayHello(@StringRes int id) {        Toast.makeText(this, "Hello " + getString(id), Toast.LENGTH_LONG).show();    }}

And what we pass to it is a style resource ID that does not match the expected string resource ID, and the IDE prompts the following warning:

Similarly, we put the warning in place using a string resource ID instead of a warning disappears:

sayHello(R.string.name);

Basically, each resource type has a corresponding resource annotation

AnimatorResAnimResAnyResArrayResAttrResBoolResColorResDimenResDrawableResFractionResIdResIntegerResInterpolatorResLayoutResMenuResPluralsResRawResStringResStyleableResStyleResXmlRes
Intdef and Stringdef annotations

The last type of annotation is based on the IntelliJ "magic number" check mechanism function

Note: "Magic number" is a numeric constant that cannot be seen, including the string constant "

Many times, for performance reasons, we use integer constants instead of enumeration types. For example, we have a Icecreamflavourmanager class, which defines three kinds of operations:

VANILLACHOCOLATESTRAWBERRY

We can define a new annotation named @flavour, and use @intdef to specify the range of values it can accept, as shown in the following example:

public class IceCreamFlavourManager {    private int flavour;    public static final int VANILLA = 0;    public static final int CHOCOLATE = 1;    public static final int STRAWBERRY = 2;    @IntDef({VANILLA, CHOCOLATE, STRAWBERRY})    public @interface Flavour {    }    @Flavour    public int getFlavour() {        return flavour;    }    public void setFlavour(@Flavour int flavour) {        this.flavour = flavour;    }}

At this point, if we use the direct literal to call icecreamflavourmanager.setflavour,ide, we will be prompted with the following error:

The IDE will even prompt us to use valid values:

We can also specify that an integer value can be used as a flag, meaning that these integer values can be manipulated with or, using ' | ' or ' & '. If we define @flavour as follows:

@IntDef(flag = true, value = {VANILLA, CHOCOLATE, STRAWBERRY})    public @interface Flavour {}

You can then make the following call:

iceCreamFlavourManager.setFlavour(IceCreamFlavourManager.VANILLA & IceCreamFlavourManager.CHOCOLATE);

@StringDef usage is almost the same as @IntDef, except for the string type value.

For more information, refer to Tools site

Android Share Q Group: 315658668

"Android" Android support Annotations

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.