(1) Android uses custom annotations to initialize controls

Source: Internet
Author: User

(1) Android uses custom annotations to initialize controls

Generally, we develop Android applications and use findViewById () to initialize controls. However, after a project is developed, you will find a lot of such code, which is actually repeated. At this time, you will find out how convenient the built-in Annotation (Annotation) in Java is.

 

1. Design a comment for ContentView

Because this annotation will be carried on the Activity class, we need to declare @ Targrt (ElementType. TYPE ).

 

[Java]
  1. @ Target (ElementType. TYPE)
  2. @ Retention (RetentionPolicy. RUNTIME)
  3.  
  4. Public @ interface ContentView {
  5. Value ();
  6. }

     

    2. Design a comment for ContentWidget

    Because this annotation is carried to the member variables of the class, we need to declare @ Targrt (ElementType. FILELD ). Class member variables specify the control objects we declare

    [Java] 
    1. @ Target (ElementType. FIELD)
    2. @ Retention (RetentionPolicy. RUNTIME)
    3. Public @ interface ContentWidget {
    4. Int value ();
    5. }

       

      3. Design a tool class to extract and process the information of the aforementioned custom Annotation class

       

      [Java] 
      1. Public static void injectObject (Object object, Activity activity ){
      2.  
      3. Class ClassType = object. getClass ();
      4.  
      5. // Whether the class has a ContentView Annotation
      6. If (classType. isAnnotationPresent (ContentView. class )){
      7. // Return the existing ContentView Annotation
      8. ContentView annotation = classType. getAnnotation (ContentView. class );
      9.  
      10.  
      11. Try {
      12.  
      13.  
      14. // Return a Method object that reflects the specified public member methods of the classes or interfaces represented by this Class object.
      15. Method method = classType
      16. . GetMethod (setContentView, int. class );
      17. Method. setAccessible (true );
      18. Int resId = annotation. value ();
      19. Method. invoke (object, resId );
      20.  
      21.  
      22. } Catch (NoSuchMethodException e ){
      23. // TODO Auto-generated catch block
      24. E. printStackTrace ();
      25. } Catch (IllegalArgumentException e ){
      26. // TODO Auto-generated catch block
      27. E. printStackTrace ();
      28. } Catch (IllegalAccessException e ){
      29. // TODO Auto-generated catch block
      30. E. printStackTrace ();
      31. } Catch (InvocationTargetException e ){
      32. // TODO Auto-generated catch block
      33. E. printStackTrace ();
      34. }
      35.  
      36.  
      37. }
      38.  
      39.  
      40. // Returns an array of the Field object. These objects reflect the member variables declared by the Class object or interface,
      41. // Includes public, protected, default (Package) Access, and private member variables, but does not include inherited member variables.
      42. Field [] fields = classType. getDeclaredFields ();
      43.  
      44.  
      45. If (null! = Fields & fields. length> 0 ){
      46.  
      47.  
      48. For (Field field: fields ){
      49. // Whether the member variable has a ContentWidget Annotation
      50. If (field. isAnnotationPresent (ContentWidget. class )){
      51.  
      52.  
      53. ContentWidget annotation = field
      54. . GetAnnotation (ContentWidget. class );
      55. Int viewId = annotation. value ();
      56. View view = activity. findViewById (viewId );
      57. If (null! = View ){
      58. Try {
      59. Field. setAccessible (true );
      60. Field. set (object, view );
      61. } Catch (IllegalArgumentException e ){
      62. // TODO Auto-generated catch block
      63. E. printStackTrace ();
      64. } Catch (IllegalAccessException e ){
      65. // TODO Auto-generated catch block
      66. E. printStackTrace ();
      67. }
      68. }
      69.  
      70.  
      71. }
      72.  
      73.  
      74. }
      75.  
      76.  
      77. }
      78.  
      79.  
      80. }

         

         

        4. How should we use the annotation defined in the Activity class?

         

         

        [Java] 
        1. @ ContentView (R. layout. activity_main)
        2. Public class MainActivity extends Activity {
        3.  
        4. @ ContentWidget (R. id. TV)
        5. Private TextView textView;
        6.  
        7. @ Override
        8. Protected void onCreate (Bundle savedInstanceState ){
        9. Super. onCreate (savedInstanceState );
        10. // SetContentView (R. layout. activity_main );
        11. ViewUtils. injectObject (this, this );
        12.  
        13. TextView. setText (custom comment );
        14. }

           

          Although there are a lot of preparations in the early stage, it will save a lot of trouble for loading layout and initializing controls during the development process in the back sequence. This is especially important for large projects, click it to cut the worker without mistake!

          DEMO address

           

           

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.