First, Introduction
What is the annotation, do not understand can first read my previous article.
In Android, annotations are mainly used to do a few things:
-
and the compiler to give you some hint warning message.
-
With some Ides, it is more convenient and efficient to write Java code efficiently and securely. Google out of the Support-annotations this library is mainly to do this.
-
Together with reflection provides some functionality similar to spring, which is convenient for brevity.
Second, support annotations chestnut
An official library is used here to illustrate a simple application in development.
2.1 Guide Pack
In the new project will be automatically guided, you can see Build.gradle in the dependency dependencies is the case.
dependencies { ‘libs‘include: [‘*.jar‘]) androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, { ‘com.android.support‘module‘support-annotations‘ }) ‘com.android.support:appcompat-v7:25.1.1‘ ‘junit:junit:4.12‘}
If not, self-reliance in Build.gradle Add (xx.x.x for your compilesdkversion version number):
‘com.android.support:support-annotations:xx.x.x‘
2.2 Use
At this point, you can use some of the annotations provided by Support-annotations, and here are some chestnuts:
- @NonNull
The test method parameter adds a nonnull annotation, and then we pass an empty argument to the past.
publicclass MainActivity extends AppCompatActivity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); null; test(s); } publicvoidtest(@NonNull String s){ System.out.println(s); }}
The IDE will prompt you for a warning
- @StringRes
The
Redefine TestString method parameter adds a stringres annotation, and then we pass a number past.
publicclass MainActivity extends AppCompatActivity { @Override protectedvoidonCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); testString(112312); testString(R.string.app_name); } publicvoidtestStringint s){ System.out.println(s); }}
The IDE will prompt you
third, the realization of their own butterknife
After prior knowledge, we already know the principle and use of annotations, here to achieve a simple function of butterknife, view injection: An annotation, a parser can.
3.1 BindView Annotations
@Target(ElementType.FIELD) //解析常量@Retention(RetentionPolicy.RUNTIME) //运行时public @interface BindView { intvaluedefault -1; //标识控件}
3.2 Bindviewparser Parser
/** * Created by LITP on 2017/2/17. * * Public class bindviewparser { /** * Pass a activty or view object, use reflection to get the view variable * @param Object * * Public Static void Inject(Object object) {Try{Parse (object); }Catch(Exception e) {E.printstacktrace (); } }/** * Parse Get value * @param Object * @throws Exception */ Public Static void Parse(Object object)throwsException {FinalClass<?> clazz = Object.getclass (); View view =NULL;//Get clazz variables, whether private or publicfield[] fields = Clazz.getdeclaredfields (); for(Field field:fields) {//Whether this variable has bindview annotations if(Field.isannotationpresent (Bindview.class)) {//Get the annotation corresponding to this variableBindView Injectview = field.getannotation (Bindview.class);//Get value intid = injectview.value ();if(ID <=0) {Throw NewException ("View ID cannot be empty"); }Else{//settings can be accessedField.setaccessible (true);//Get View if(ObjectinstanceofView) {view = ((view) object). Findviewbyid (ID); }Else if(ObjectinstanceofActivity) {view = (activity) object. Findviewbyid (ID); }//Set ViewField.set (object, view); } } } }}
3.3 Activity Use
Public class mainactivity extends appcompatactivity { //Use annotations to identify variables @BindView(r.id.tv_test) TextView TextView;@Override protected void onCreate(Bundle savedinstancestate) {Super. OnCreate (Savedinstancestate); Setcontentview (R.layout.activity_main);//Pass current activty to parser, initialize viewBindviewparser.inject ( This);//This is already initialized and can be usedTextview.settext ("Test Your own injection demo"); }}
Android AOP Road three annotations on Android