zoukankan      html  css  js  c++  java
  • xUtils怎样通过注解对FindViewById进行封装

      之前讲到了介绍了一下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群:JRedu技术交流 (452379712)

    作者:杰瑞教育
    出处:http://www.cnblogs.com/jerehedu/ 
    本文版权归烟台杰瑞教育科技有限公司和CSDN共同拥有,欢迎转载,但未经作者允许必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。

     
  • 相关阅读:
    Solr7.7安装,配置中文分词 IKAnalyzer分词
    Solr7.7安装,链接数据库并导入数据,初级导入数据
    Solr7.7安装,过浏览器可以看到Solr界面,进行可视化操作
    Solr7.7安装,JDK安装
    Jquery 全局错误处理
    各种问题
    Elmah 数据库脚本
    LINQ&EF任我行(二)--LinQ to Object
    css3 animatehue属性
    Lync二次开发关于Lync启动退出问题
  • 原文地址:https://www.cnblogs.com/gccbuaa/p/6746810.html
Copyright © 2011-2022 走看看