Android support Annotations using the detailed

Source: Internet
Author: User
<span id="Label3"></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; line-height:25px"><span style="font-size:18px"><strong>Original Link: http://www.codeceo.com/article/android-support-annotations-2.html</strong></span></p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">In the Android support Library19.1 release, the Android tools team introduced several cool annotation types for developers to use in the Project. The support library itself also uses these annotations, which is a good omen. Let's take a good look at it.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">These annotations can be easily added to our project by gradle:</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)"><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">Compile ' com.android.support:support-annotations:20.0.0 '</pre></pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">There are three types of annotations available for us to use:</p></p> <ul style="margin:0px 0px 16px 26px; padding:0px; list-style:none; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"> <ul style="margin:0px 0px 16px 26px; padding:0px; list-style:none; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"> <li style="margin:0px 0px 10px; padding:0px 0px 0px 20px">Nullness annotations;</li> <li style="margin:0px 0px 10px; padding:0px 0px 0px 20px">Resource Type annotations;</li> <li style="margin:0px 0px 10px; padding:0px 0px 0px 20px">Intdef and Stringdef annotations;</li> </ul> </ul><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">We will use code examples to explain the role of each type and how they are used in engineering.</p></p>nullness annotations<p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">Parameters decorated with @nonnull annotations cannot be Null. In the following code example, we have a null name variable that is passed as a parameter to the SayHello function, which requires that the parameter be a non-null string type:</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">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 s)  {         toast.maketext (this,  "hello "  + s, toast.length_long). Show ();     }}</pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">Because the parameter string s in the code uses the @nonnull annotation decoration, the IDE warns us of a problem with this place in the form of a warning:</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"></p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">If we assign a value to name, such as String name = "us Lord Duarte", then the warning Disappears. function parameters that are decorated with @nullable annotations or return values can be Null. Assuming that the User class has a variable named name, using User.getname () access, We can write the following code:</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">public class mainactivity extends actionbaractivity {    @ Override    protected void oncreate (bundle savedinstancestate)  {         super.oncreate (savedinstancestate);         setcontentview (r.layout.activity_main);         user  user = new user ("our lord duarte");         toast.maketext (this,  "hello "  + getname (user),  toast.length_long). show ();     }     @Nullable     string getname (@NonNull   User user)  {        return user.getname ();     }}</pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">Because the return value of the GetName function is decorated with @nullable, it is called:</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)"><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">Toast.maketext (this, "Hello" + getName (user), toast.length_long). show ();</pre></pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">Not checking whether the return value of GetName is empty will likely result in Crash.</p></p>Resource type annotations<p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; Color:rgb (68,68,68); Font-family: ' Microsoft Yahei '; font-size:14px; line-height:25px "> Have you ever passed the wrong resource integer value to the function, and can you happily get the integer value you want? Resource type annotations can help us achieve this accurately. In the following code, our SayHello function expects to accept the ID of a string type and is decorated with the @stringres annotation: </p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">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 (); }}</pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">And we passed a style resource ID to it, and the IDE will prompt the following warning:</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"></p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">similarly, we put the warning in place using a string resource ID instead of a warning disappears:</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)"><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">SayHello (r.string.name);</pre></pre>Intdef and Stringdef annotations<p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">The last type of annotation we want to cover is the intellij-based "magic constant" check mechanism (http://blog.jetbrains.com/idea/2012/02/new-magic-constant-inspection/)<br>(we don't need to know more about how this mechanism is implemented, but you can click on the link if you want to know).</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">Many times, we use integer constants instead of enumeration types (performance considerations), such as we have a icecreamflavourmanager class, which has three modes of operation: vanilla,chocolate and Strawberry. We can define a new annotation named @flavour and use @intdef to specify the type of value it can accept.</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">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;    }}</pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">At this point, if we call Icecreamflavourmanager.setflavour using the wrong integer value, the IDE will make an error as Follows:</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"></p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">The IDE will even prompt us to use valid values:</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"></p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">We can also specify an integer value as the flag bit, meaning that these integer values can be manipulated with or, using ' | ' or ' & '. If we define @flavour as the following flag bit:</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)"><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">@IntDef (flag = true, value = {VANILLA, chocolate, strawberry}) public @interface Flavour {}</pre></pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">Then you can call the Following:</p></p><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)"><pre style="margin:15px auto; padding:10px 15px; word-break:break-all; word-wrap:break-word; line-height:20px; font-family:‘courier new‘; border-width:1px 1px 1px 4px; border-style:solid; border-color:rgb(221,221,221); color:rgb(68,68,68); background:rgb(251,251,251)">Icecreamflavourmanager.setflavour (icecreamflavourmanager.vanilla & Icecreamflavourmanager. chocolate);</pre></pre><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">@StringDef usage is roughly the same as @intdef, except for string types.</p></p><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px"><p style="margin-top:0px; margin-bottom:15px; padding-top:0px; padding-bottom:0px; color:rgb(68,68,68); font-family:‘microsoft yahei‘; font-size:14px; line-height:25px">For questions such as what new annotation types will be added in the future, or how these annotations are dependent, and how annotations interact with IntelliJ itself, You can view the Url: http://tools.android.com/tech-docs/support-annotations.</p></p> <p style="font-size:12px;"><p style="font-size:12px;">Copyright Notice: This article for Bo Master original article, without Bo Master permission not Reproduced.</p></p> <p><p>Android support Annotations using the detailed</p></p></span>

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.