Android注解学习(2)
? ? 最近考試周沒什么時(shí)間寫,回歸正題。前面的一次簡(jiǎn)單的講了關(guān)于注解的的基礎(chǔ)部分,這一次分析xutils注解封裝的源碼(奉上github源碼)。
? ? 補(bǔ)充下:xUtils 2.x對(duì)Android 6.0兼容不是很好, 請(qǐng)盡快升級(jí)至xUtils3。(如網(wǎng)絡(luò)部分:android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留幾個(gè)類))
? ?(1) 注解模塊目錄結(jié)構(gòu)
? ?(2) 分析源代碼
? 1.元注解--annoation文件夾下三個(gè)注解
? ?ContentView:對(duì)于activity設(shè)置布局文件
/** Copyright (c) 2013. wyouflf (wyouflf@gmail.com)** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.xutils.view.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target(ElementType.TYPE)//使用范圍--類,接口,枚舉,Annotation類型 @Retention(RetentionPolicy.RUNTIME)//有效范圍--運(yùn)行時(shí)有效 public @interface ContentView {int value();//布局資源 }Event:
package org.xutils.view.annotation;import android.view.View;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;/*** 事件注解.* 被注解的方法必須具備一下形式:* 1. private 修飾* 2. 返回值類型沒有要求* 3. 方法名以Click或Event結(jié)尾, 否則可能被混淆編譯時(shí)刪除. (方法簽名形式為void *(android.view.View)也可以)* 4. 參數(shù)簽名和type的接口要求的參數(shù)簽名一致.* Author: wyouflf* Date: 13-9-9* Time: 下午12:43*/ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Event {/*** 控件的id集合, id小于1時(shí)不執(zhí)行ui事件綁定.** @return*/int[] value();/*** 控件的parent控件的id集合, 組合為(value[i], parentId[i] or 0).** @return*/int[] parentId() default 0;/*** 事件的listener, 默認(rèn)為點(diǎn)擊事件.** @return*/Class<?> type() default View.OnClickListener.class;/*** 事件的setter方法名, 默認(rèn)為set+type#simpleName.** @return*/String setter() default "";/*** 如果type的接口類型提供多個(gè)方法, 需要使用此參數(shù)指定方法名.** @return*/String method() default ""; }ViewInject:綁定控件id
/** Copyright (c) 2013. wyouflf (wyouflf@gmail.com)** Licensed under the Apache License, Version 2.0 (the "License");* you may not use this file except in compliance with the License.* You may obtain a copy of the License at** http://www.apache.org/licenses/LICENSE-2.0* Unless required by applicable law or agreed to in writing, software* distributed under the License is distributed on an "AS IS" BASIS,* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.* See the License for the specific language governing permissions and* limitations under the License.*/package org.xutils.view.annotation;import java.lang.annotation.ElementType; import java.lang.annotation.Retention; import java.lang.annotation.RetentionPolicy; import java.lang.annotation.Target;@Target(ElementType.FIELD)//字段 @Retention(RetentionPolicy.RUNTIME)//運(yùn)行時(shí)有效 public @interface ViewInject {int value();//綁定控件id/* parent view id */int parentId() default 0;//綁定控件父布局id 默認(rèn)是取0 }?2.View文件夾下4個(gè)類
ViewInfo--1.控件注解生成ViewInfo;2.對(duì)于散列集合包括HashSet、HashMap以及HashTable通過先比較hashcode相等再比較equals來提高效率 package org.xutils.view;/*** 比較兩個(gè)ViewInfo是否相等* Author: wyouflf* Date: 13-12-5* Time: 下午11:25*/ /*package*/ final class ViewInfo {public Object value;public int parentId;@Overridepublic boolean equals(Object o) {if (this == o) return true;if (!(o instanceof ViewInfo)) return false;ViewInfo that = (ViewInfo) o;if (parentId != that.parentId) return false;if (value == null) return (null == that.value);return value.equals(that.value);}@Overridepublic int hashCode() {int result = value.hashCode();result = 31 * result + parentId;return result;} }
?ViewInjector--定義注解綁定的接口不再詳細(xì)貼了
?ViewInjectorImpl(重點(diǎn))--實(shí)現(xiàn)注解綁定接口的實(shí)現(xiàn)類(包括控件綁定,布局資源綁定,控件的事件綁定)
?使用單例模式雙重檢查鎖定創(chuàng)建ViewInjectorImpl對(duì)象并保證線程安全
?簡(jiǎn)單的做了ViewInjectorImpl類的分析:
?
錯(cuò)誤之處還請(qǐng)指出-_-。。。
?
轉(zhuǎn)載于:https://www.cnblogs.com/lmf-techniques/p/5139602.html
總結(jié)
以上是生活随笔為你收集整理的Android注解学习(2)的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: iPhone手机桌面截图?各种手机截屏快
- 下一篇: iPhone手机掉进水里后应该这么办?