日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > Android >内容正文

Android

Android注解学习(2)

發布時間:2023/12/19 Android 39 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Android注解学习(2) 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

? ? 最近考試周沒什么時間寫,回歸正題。前面的一次簡單的講了關于注解的的基礎部分,這一次分析xutils注解封裝的源碼(奉上github源碼)。

? ? 補充下:xUtils 2.x對Android 6.0兼容不是很好, 請盡快升級至xUtils3。(如網絡部分:android 6.0(api 23) SDK,不再提供org.apache.http.*(只保留幾個類))

? ?(1) 注解模塊目錄結構

? ?(2) 分析源代碼

? 1.元注解--annoation文件夾下三個注解

? ?ContentView:對于activity設置布局文件

/** 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)//有效范圍--運行時有效 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結尾, 否則可能被混淆編譯時刪除. (方法簽名形式為void *(android.view.View)也可以)* 4. 參數簽名和type的接口要求的參數簽名一致.* Author: wyouflf* Date: 13-9-9* Time: 下午12:43*/ @Target(ElementType.METHOD) @Retention(RetentionPolicy.RUNTIME) public @interface Event {/*** 控件的id集合, id小于1時不執行ui事件綁定.** @return*/int[] value();/*** 控件的parent控件的id集合, 組合為(value[i], parentId[i] or 0).** @return*/int[] parentId() default 0;/*** 事件的listener, 默認為點擊事件.** @return*/Class<?> type() default View.OnClickListener.class;/*** 事件的setter方法名, 默認為set+type#simpleName.** @return*/String setter() default "";/*** 如果type的接口類型提供多個方法, 需要使用此參數指定方法名.** @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)//運行時有效 public @interface ViewInject {int value();//綁定控件id/* parent view id */int parentId() default 0;//綁定控件父布局id 默認是取0 }

?2.View文件夾下4個類

ViewInfo--1.控件注解生成ViewInfo;
2.對于散列集合包括HashSet、HashMap以及HashTable通過先比較hashcode相等再比較equals來提高效率
package org.xutils.view;/*** 比較兩個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--定義注解綁定的接口不再詳細貼了

?ViewInjectorImpl(重點)--實現注解綁定接口的實現類(包括控件綁定,布局資源綁定,控件的事件綁定)

?使用單例模式雙重檢查鎖定創建ViewInjectorImpl對象并保證線程安全

?簡單的做了ViewInjectorImpl類的分析:

?

錯誤之處還請指出-_-。。。

?

轉載于:https://www.cnblogs.com/lmf-techniques/p/5139602.html

總結

以上是生活随笔為你收集整理的Android注解学习(2)的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。