Android 注解的一些应用以及原理

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

简单来讲,注解这个东西就是用于辅助咱们开发java代码的,注解自己没法干扰java源代码的执行。java

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

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

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

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

 

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

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

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

而后咱们就能够看看这个谷歌的注解库 为咱们提供了哪些功能。ide

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

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

前者表示能够为空 后者表示不能为空。

我只演示后者(前者能够本身测试)定义一个函数

而后咱们去调用他 看看。

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和编译器是没有反应的,到运行期才会有错误。

可是你加了之后就会发现

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就会很明确的提示你错误

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

那这个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 }

而后定义一个解释器

 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 }
相关文章
相关标签/搜索