(1) Android uses custom annotations to initialize controls. android Initialization

Source: Internet
Author: User
Tags array definition

(1) Android uses custom annotations to initialize controls. android Initialization

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]View plaincopyprint?
  1. @ Target (ElementType. TYPE)
  2. @ Retention (RetentionPolicy. RUNTIME)
  3. Public @ interface ContentView {
  4. Value ();
  5. }

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


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

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

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



How do android custom controls input values during initialization?

If you use code, just pass the value to the constructor.
Configure and add attributes by yourself in xml, and assign values to attributes


Android control array definition and usage Problems

Bv = new Button [10];
You just created an array to store 10 buttons.
However, you have not initialized the Button.
Bv [I] = null; changed to bv [I] = new Button (this );

Related Article

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.