博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
xUtils怎样通过注解对FindViewById进行封装
阅读量:6891 次
发布时间:2019-06-27

本文共 4140 字,大约阅读时间需要 13 分钟。

  之前讲到了介绍了一下xUtils的基本用法,今天我们就来具体介绍一下关于xUtils中的ViewUtils模块。

  在ViewUtils模块中我们首先看到的是它採用了一种注解的方式进行声明,那么我们首先来了解一下什么是注解。

  注解(Annotation)非常重要,未来的开发模式都是基于注解的。JPA是基于注解的。Spring2.5以上都是基于注解的。Hibernate3.x以后也是基于注解的,如今的Struts2有一部分也是基于注解的了,注解是一种趋势,如今已经有不少的人開始用注解了。注解是JDK1.5之后才有的新特性。

  JDK1.5之后内部提供的三个注解

       @Deprecated 意思是“废弃的,过时的”

       @Override 意思是“重写、覆盖”

       @SuppressWarnings 意思是“压缩警告”

  我们首先来了解一下JDK内部提供的注解中比較难懂的一个SuppressWarnings,这是从jdk1.5的文档中找到的SuppressWarnings的值

all  - suppress all warnings from this code  

deprecation  - suppress warnings from using deprecated code  

unchecked - suppress warnings from an unchecked call or an unchecked cast  

fallthrough - suppress warnings if a switch falls through without finding a valid case (and no default)  

serial - suppress warnings if a Serializable class does not define a serialVersionUID  

finally - suppress warnings from return within a finally (which will ignore return with the try) 

  演示样例:

·   @SuppressWarnings("unchecked")

告诉编译器忽略 unchecked 警告信息。如使用List,ArrayList等未进行參数化产生的警告信息。

·   @SuppressWarnings("serial")

假设编译器出现这种警告信息:The serializable class WmailCalendar does not declare a static final serialVersionUID field of type long

       使用这个凝视将警告信息去掉。

·   @SuppressWarnings("deprecation")

假设使用了使用@Deprecated凝视的方法,编译器将出现警告信息。

       使用这个凝视将警告信息去掉。

·   @SuppressWarnings("unchecked", "deprecation")

告诉编译器同一时候忽略unchecked和deprecation的警告信息。

·   @SuppressWarnings("unused")

告诉编译器同一时候忽略没实用过的变量

  举个样例

  定义一个变量的时候会出现警告 The local variable jerehedu is never read。 警告我们的变量没有被用过。当我们加上注解@SuppressWarnings("unused")

  警告就没有了。这个就是注解的作用。

     了解了注解的作用,我们再准备自己写一个注解,我们首先来看一下在ViewUtils模块中它的注解是怎么写的,以@ViewInject为例。

  这个是viewinject的源代码,在我们的ViewUtils中我们能够看到

 
// inject view        Field[] fields = handlerType.getDeclaredFields();        if (fields != null && fields.length > 0) {            for (Field field : fields) {                ViewInject viewInject = field.getAnnotation(ViewInject.class);                if (viewInject != null) {                    try {                        View view = finder.findViewById(viewInject.value(), viewInject.parentId());                        if (view != null) {                            field.setAccessible(true);                            field.set(handler, view);                        }                    } catch (Throwable e) {                        LogUtils.e(e.getMessage(), e);                    }                } else {                    ResInject resInject = field.getAnnotation(ResInject.class);                    if (resInject != null) {                        try {                            Object res = ResLoader.loadRes(                                    resInject.type(), finder.getContext(), resInject.id());                            if (res != null) {                                field.setAccessible(true);                                field.set(handler, res);                            }                        } catch (Throwable e) {                            LogUtils.e(e.getMessage(), e);                        }                    } else {                        PreferenceInject preferenceInject = field.getAnnotation(PreferenceInject.class);                        if (preferenceInject != null) {                            try {                                Preference preference = finder.findPreference(preferenceInject.value());                                if (preference != null) {                                    field.setAccessible(true);                                    field.set(handler, preference);                                }                            } catch (Throwable e) {                                LogUtils.e(e.getMessage(), e);                            }                        }                    }                }            }        }

  首先获得文件的注解为ViewInject

  ViewInject viewInject = field.getAnnotation(ViewInject.class);

  将我们通常所写的findViewById封装在这里

  findviewbyidView view = finder.findViewById(viewInject.value(), viewInject.parentId());

  至于怎样封装在我们的ViewFinder中能够找到

  这就是我们的xUtils怎样通过注解的方式来完毕对findViewById这种方法的二次封装

 

  疑问咨询或技术交流,请增加官方QQ群: (452379712)

 

作者:
出处:
 
本文版权归和CSDN共同拥有,欢迎转载,但未经作者允许必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

 
你可能感兴趣的文章
一、数组二三
查看>>
Android_触摸设备
查看>>
mysql读书笔记(三)
查看>>
实例:调用系统字体
查看>>
程序员应该重视版本控制
查看>>
提升Salt Api稳定性
查看>>
sqoop架构原理与操作
查看>>
C Primer Plus 第5章 运算符、表达式和语句 5.6 带有参数的函数
查看>>
js 函数节流与函数防抖技巧
查看>>
Netty概述
查看>>
PAT 1010__已过但why二分查找时mid须初始化为low
查看>>
1079 Total Sales of Supply Chain
查看>>
Linux文件和目录管理(1)
查看>>
nginx+tomcat集群+redis(memcache)session共享
查看>>
Python爬虫实战之“网易云音乐绝对互粉”
查看>>
centos7-nagiospnp-4.08配置
查看>>
数据持久化一之属性列表
查看>>
directx 11 64位
查看>>
颜色的渐变 hsla
查看>>
pChart的使用总结
查看>>