转载自:http://www.cnblogs.com/punkisnotdead/p/4705258.html

在这边文章之前你首先需要对java 的注解部分有一个基本的了解(不需要太过的深入)。

简单来说,注解这个东西就是用于辅助我们开发java代码的,注解本身无法干扰java源代码的执行。

在android 里面 注解主要用来干这么几件事:

1.和编译器一起给你一些提示警告信息。

2.配合一些ide 可以更加方便快捷 安全有效的编写java代码。谷歌出的support-annotations这个库 就是主要干这个的。

3.和反射一起 提供一些类似于spring 可配置的功能,方便简洁(这部分有过j2ee 开发经验的可以直接跳过了)。

 

首先来看一下官方提供的库吧。首先在build文件里 增加一句话

compile 'com.android.support:support-annotations:22.2.0'

这个地方要注意,我现在是取得version22.2的版本,你们引用的时候如果有更新,android studio可能会提示你sync失败,这个时候你要自己去谷歌官网查询一下最新的版本号自己替换。

然后我们就可以看看这个谷歌的注解库 为我们提供了哪些功能。

http://tools.android.com/tech-docs/support-annotations 这个是官方的文档 我也是主要根据这个来讲解这个库的使用。

首先来看 Nullness 注解 这个主要是有两种@Nullable  和 @NonNull。

前者表示可以为空 后者表示不能为空。

我只演示后者(前者可以自己测试)定义一个函数

Android 注解助你理解AS和Butterknife

然后我们去调用他 看看。

1  String a=null;
2         sayHello(a);

 

这个时候IDE就自动会给我们警告信息了(其实ide 的警告信息多数也是从编译器而来)


1 Argument 'a' might be null less... (Ctrl+F1) 
2 This inspection analyzes method control and data flow to report possible conditions that are always true or false, expressions whose value is statically proven to be constant, and situations that can lead to nullability contract violations.
3 Variables, method parameters and return values marked as @Nullable or @NotNull are treated as nullable (or not-null, respectively) and used during the analysis to check nullability contracts, e.g. report possible NullPointerException errors.
4 More complex contracts can be defined using @Contract annotation, for example:
5 @Contract("_, null -> null") — method returns null if its second argument is null @Contract("_, null -> null; _, !null -> !null") — method returns null if its second argument is null and not-null otherwise @Contract("true -> fail") — a typical assertFalse method which throws an exception if true is passed to it 
6 The inspection can be configured to use custom @Nullable
7 @NotNull annotations (by default the ones from annotations.jar will be used)

再介绍一下

Resource Type Annotations

定义一个方法

1  private String sayHello(@StringRes int resId) {
2         return "";
3     }

然后调用

1         sayHello(R.layout.activity_main);

这个地方这个注解的意思是你只能传string 类型的id进去 而我们传了layout类型的,如果你不加那个注解的话 ide和编译器是没有反应的,到运行期才会有错误。

但是你加了以后就会发现

Android 注解助你理解AS和Butterknife

IDE也是直接报错的!

 

再比如这样一段代码


 1  class TestAnoataion {
 2 
 3         public void testAnoation() {
 4             Log.v("main", "test anoation");
 5         }
 6     }
 7 
 8     class TestAnoataion2 extends TestAnoataion
 9     {
10         public void testAnoation()
11         {
12 
13         }
14     }

假使我们父类的设计的时候本意是 如果你要重写testAnoation这个方法一定得调用父类的testAnation这个方法。就跟我们activity的oncreate方法一样。

哪如果子类没有写super.testAnoation的话 你根本就不知道 就很容易报错。但是如果你加了注解。那么ide就会很明确的提示你错误

Android 注解助你理解AS和Butterknife

是不是很强大?当你调用了super语句以后 一切就和谐了!

Android 注解助你理解AS和Butterknife

那这个support库 就暂时介绍到这里,个人建议大家有时间一定要自己过一遍一个官方文档。

这里定义的注解 对提高大家的代码质量 会非常非常有帮助。一定要掌握学会。

 

最后再上一个小例子吧,很多android 开发者都喜欢依赖注入的一些开源库,来绑定你的控件id。

这样写法不但优雅而且好用。

Butterknife

这个开源库大家一定很多人都用过,我就写一个小demo 来演示一下这些库的原理,其实还蛮简单的。

需要注意的是 要理解这个小demo 需要你除了知道注解的基础知识以外还需要有反射的基础知识。

关于反射我很久之前也写过一个教程

http://www.cnblogs.com/punkisnotdead/p/3384464.html

不会的可以赶紧补一下。

首先定义一个InjectView注解


 1 package com.example.administrator.testfab;
 2 
 3 import java.lang.annotation.ElementType;
 4 import java.lang.annotation.Retention;
 5 import java.lang.annotation.RetentionPolicy;
 6 import java.lang.annotation.Target;
 7 
 8 /**
 9  * Created by Administrator on 2015/8/5.
10  */
11 
12 @Target(ElementType.FIELD)
13 @Retention(RetentionPolicy.RUNTIME)
14 public @interface InjectView {
15     //id就表示哪些控件,-1就表示取不到时候的默认值
16     int id() default -1;
17 }
Android 注解助你理解AS和Butterknife

然后定义一个解释器


 1 package com.example.administrator.testfab;
 2 
 3 import android.app.Activity;
 4 import android.view.View;
 5 
 6 import java.lang.reflect.Field;
 7 
 8 /**
 9  * Created by Administrator on 2015/8/5.
10  */
11 public class InjectViewParser {
12 
13     public static void inject(Object object) {
14 
15         try {
16             parse(object);
17         } catch (Exception e) {
18             e.printStackTrace();
19         }
20     }
21 
22     public static void parse(Object object) throws Exception {
23         final Class<?> clazz = object.getClass();
24         View view = null;
25         Field[] fields = clazz.getDeclaredFields();
26         for (Field field : fields) {
27             if (field.isAnnotationPresent(InjectView.class)) {
28                 InjectView injectView = field.getAnnotation(InjectView.class);
29                 int id = injectView.id();
30                 if (id < 0) {
31                     throw new Exception("id must not be null");
32                 } else {
33                     field.setAccessible(true);
34                     if (object instanceof View) {
35                         view = ((View) object).findViewById(id);
36                     } else if (object instanceof Activity) {
37                         view = ((Activity) object).findViewById(id);
38                     }
39                     field.set(object, view);
40                 }
41 
42             }
43 
44         }
45 
46     }
47 }

最后在actity里使用即可

 


 1 package com.example.administrator.testfab;
 2 
 3 import android.os.AsyncTask;
 4 import android.os.Bundle;
 5 import android.support.annotation.CallSuper;
 6 import android.support.annotation.NonNull;
 7 import android.support.annotation.Nullable;
 8 import android.support.annotation.StringRes;
 9 import android.support.v7.app.AppCompatActivity;
10 import android.util.Log;
11 import android.view.Menu;
12 import android.view.MenuItem;
13 import android.widget.Button;
14 
15 public class MainActivity extends AppCompatActivity {
16 
17     @InjectView(id = R.id.bt)
18     private Button bt;
19 
20 
21     @Override
22     protected void onCreate(Bundle savedInstanceState) {
23         super.onCreate(savedInstanceState);
24         setContentView(R.layout.activity_main);
25         //开始注入
26         InjectViewParser.inject(this);
27         //这个主要是测试注入id 成功没有 成功了就不会报错~
28         bt.setText("inject done");
29 
30 
31     }
32 
33 
34     @Override
35     public boolean onCreateOptionsMenu(Menu menu) {
36         // Inflate the menu; this adds items to the action bar if it is present.
37         getMenuInflater().inflate(R.menu.menu_main, menu);
38         return true;
39     }
40 
41     @Override
42     public boolean onOptionsItemSelected(MenuItem item) {
43         // Handle action bar item clicks here. The action bar will
44         // automatically handle clicks on the Home/Up button, so long
45         // as you specify a parent activity in AndroidManifest.xml.
46         int id = item.getItemId();
47 
48         //noinspection SimplifiableIfStatement
49         if (id == R.id.action_settings) {
50             return true;
51         }
52 
53         return super.onOptionsItemSelected(item);
54     }
55 
56 
57 
58 }

分类:

技术点:

相关文章: