(一)Android使用自定義註釋來初始化控件

 一般情況下我們開發Android應用,初始化控件使用findViewById(),可是一個項目開發完畢,你會發現很多這樣的代碼,其實是重複的。這個時候你就會發現Java自帶的註釋(Annotation)是多麼的方便了。

      

       一、設計一個ContentView的註釋

          因爲這個註釋我們要運在Activity類上,所以我們要申明@Targrt(ElementType.TYPE)。

   

  1.  @Target(ElementType.TYPE)  
  2.  @Retention(RetentionPolicy.RUNTIME)  
  3.   
  4.  public @interface ContentView {  
  5.  value();  
  6. }  
  

    二、設計一個ContentWidget的註釋

        因爲這個註釋我們要運在類的成員變量上,所以我們要申明@Targrt(ElementType.FILELD)。類成員變量指定我們申明的控件對象

  1. @Target(ElementType.FIELD)  
  2. @Retention(RetentionPolicy.RUNTIME)  
  3. public @interface ContentWidget {  
  4.     int value();  
  5. }  
  

  三、設計一個工具類來提取並處理上述自定義的Annotation類的信息

  

  1.      public static void injectObject(Object object, Activity activity) {  
  2.   
  3. Class<?> classType = object.getClass();  
  4.   
  5. // 該類是否存在ContentView類型的註解  
  6. if (classType.isAnnotationPresent(ContentView.class)) {  
  7. // 返回存在的ContentView類型的註解  
  8. ContentView annotation = classType.getAnnotation(ContentView.class);  
  9.   
  10.   
  11. try {  
  12.   
  13.   
  14. // 返回一個 Method 對象,它反映此 Class 對象所表示的類或接口的指定公共成員方法。  
  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. // 返回 Field 對象的一個數組,這些對象反映此 Class 對象表示的類或接口聲明的成員變量,  
  41. // 包括公共、保護、默認(包)訪問和私有成員變量,但不包括繼承的成員變量。  
  42. Field[] fields = classType.getDeclaredFields();  
  43.   
  44.   
  45. if (null != fields && fields.length > 0) {  
  46.   
  47.   
  48. for (Field field : fields) {  
  49. // 該成員變量是否存在ContentWidget類型的註解  
  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. }  


    四、在Activity類中我們該怎麼使用我們定義的註釋

           

  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(thisthis);  
  12.   
  13.         textView.setText("自定義註釋");  
  14.     }  
 

雖然前期準備的工作有點多,但後序我們的開發過程中對於加載佈局和初始化控件會省事不少,這個對於大型項目來說,尤爲重要,磨刀不誤砍材工!

  DEMO 地址


     

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章