一  xutils框架引入到AndroidStudio工程,最简单的方法:
① 在APP的build.gradle里面加入 compile 'org.xutils:xutils:3.3.36'。
② 一定要自定义一个application.class
在自定义的application里的onCreate方法里面初始化xutils:

x.Ext.init(this);//初始化XUtils
x.Ext.setDebug(BuildConfig.DEBUG);//是否输出debug日志

③ 在manifest文件里面加入自己的application

 
<application
android:name=".application.GlobalApplication"
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
 

④ 加入权限 
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

其他的方法比如引入jar包引入lib自行百度。

二 xutils框架的注解
注解框架很好用,直接在activity或者fragment里面加入
@ViewInject(R.id.xxx)
private TextView mTvText;
注意,注解的时候一定要在onCreate之前,像声明属性和变量一样。然后偶就好奇注解框架是怎么完成了用viewInject去完成findViewById这个操作的。就顺便了解了一下注解是怎么一回事。

  2.1 注解机制  其实就是java的反射实现的,我不在这里掉书袋解释反射是啥,自行百度。下面就自己实现了一下用反射实现findViewById
  ① 新建一个@annotation类型的class ,如图 ,

我们看到ViewInject的创建和接口差不多,但是别忘了前面的@和前面的声明@Target和@Retention

@Target(ElementType.FIELD)
@Retention(RetentionPolicy.RUNTIME)
public @interface ViewInject {
int id () default -1;//控件ID,在某一个控件上面使用注解标注其ID
}

然后就是反射类InjectView的创建。类里面只有一个方法injectView(Activity activity)

 
public class InjectView {
public static void injectView(Activity activity) {
try {
Class clazz = activity.getClass();
Field[] fields = clazz.getDeclaredFields();//得到传入的activity的所有字段
for(Field field:fields){
if(field.isAnnotationPresent(ViewInject.class)){
ViewInject inject = field.getAnnotation(ViewInject.class);
int id = inject.id();
if(id > 0 ){
field.setAccessible(true);
field.set(activity,activity.findViewById(id));
} }
}
}catch (IllegalAccessException e){
e.printStackTrace();
}catch (IllegalArgumentException e){
e.printStackTrace();
} }
}
 

然后再在我的activity中使用我自己定义的注解

 
public class MainActivity extends AppCompatActivity {

    @ViewInject(id = R.id.tv_text)
private TextView mTvText; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
InjectView.injectView(this);
mTvText.setText("ssssss");
}
}
 

可以看到我先注解声明我的TextView,完成注解声明后再利用InjectView.injectView(this),来完成整个注解。类里面用到了一个Field的类,就又好奇这个是干啥的(好奇宝宝),在刚接触java的时候书上看到过Field就是对象的属性,当时觉得就这么回事吧,但素,后面学习的时候又见过几回,那时候没怎么去真正的去深入了解,只知道跟反射有点关系,在网络编程和数据库挂钩的一些代码编写里面出现的频率太大!!!当时正年少轻狂,什么都是只是浅尝辄止。如今追悔莫及!!扼腕长叹!!!不废话了,接下来熟悉Field

  2.2 Field类的学习

  在查看了一些文档并自己写了一个测试类之后,才明白,Field说白了其实就是类的属性,是啊说的很在理(书上说的是没错的所以不能说书糊弄了你我)。通过它可以得到一个类的所有属性的类型,父类型以及属性值等等。

下面就是我写的那个测试类:首先写一个Student的实体

 
package bean;

import java.util.Date;

public class Students {

    private String name ;
private String address;
private long id;
private Date birthday;
public String nickName; @Override
public String toString() {
return "Students [name=" + name + ", address=" + address + ", id=" + id
+ ", birthday=" + birthday + ", nickName=" + nickName + "]";
}
public Date getBirthday() {
return birthday;
}
public void setBirthday(Date birthday) {
this.birthday = birthday;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getNickName() {
return nickName;
}
public void setNickName(String nickName) {
this.nickName = nickName;
} }
 

然后就是针对这个实体进行Field的测试

 
package main;

import java.lang.reflect.Field;
import java.util.Date; import bean.Students; public class TestField { public static void main(String[] args) {
Students student = new Students();
student.setName("John");
student.setNickName("pumpKing");
student.setBirthday(new Date());
student.setId(3501197405300359L); try {
Field property1 = student.getClass().getDeclaredField("name");
Field property2 = student.getClass().getField("nickName");
Field property3 = student.getClass().getDeclaredField("birthday");
System.err.println(property1);
System.err.println(property2);
System.err.println(property3);
System.out.println("the superClass of Student is: "+student.getClass().getSuperclass());
System.out.println("the superClass of String is: "+property1.getClass().getSuperclass());
System.out.println("the superClass of Date is: "+property3.getClass().getSuperclass());
System.out.println("the classLoader of student is: "+student.getClass().getClassLoader().getClass().getName());
Field[] fields = student.getClass().getDeclaredFields();
for(Field field : fields){
field.setAccessible(true);
System.out.println(field.get(student));
System.out.println(field);
}
System.out.println("origin = "+student.toString());
property2.set(student, "egg");//利用反射操作类的属性
System.out.println("then = "+student.toString());
} catch (NoSuchFieldException e) {
e.printStackTrace();
} catch (SecurityException e) {
e.printStackTrace();
} catch (IllegalArgumentException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
}
}
 

下面是测试的结果

 
private java.lang.String bean.Students.name
public java.lang.String bean.Students.nickName
private java.util.Date bean.Students.birthday
the superClass of Student is: class java.lang.Object
the superClass of String is: class java.lang.reflect.AccessibleObject
the superClass of Date is: class java.lang.reflect.AccessibleObject
the classLoader of student is: sun.misc.Launcher$AppClassLoader
John
private java.lang.String bean.Students.name
null
private java.lang.String bean.Students.address
3501197405300359
private long bean.Students.id
Tue Jun 14 14:45:45 CST 2016
private java.util.Date bean.Students.birthday
pumpKing
public java.lang.String bean.Students.nickName
origin = Students [name=John, address=null, id=3501197405300359, birthday=Tue Jun 14 14:45:45 CST 2016, nickName=pumpKing]
then = Students [name=John, address=null, id=3501197405300359, birthday=Tue Jun 14 14:45:45 CST 2016, nickName=egg]
 

okay,XUTils的注解就说到这里,毕竟这并不是它的重头戏。

XUtils框架的学习(一)的更多相关文章

  1. xUTils框架的学习(二)

    这章讲的是框架的DbUtils模块的学习 三 xUtils框架的DButils模块 最开始接触这个框架就是从数据库模块开始的.当时的需求是需要记录用户的登录数据,保存在本地以便进行离线登录.首先想到的 ...

  2. XUTils框架的学习(三)

    前面两章说了xutils框架的引入和注解模块的使用和数据库模块的使用,想了解的朋友可以去看看. 前面在说数据库模块的操作的时候是手动创建数据库并保存在asset文件夹里面,再通过I/O将数据库写进应用 ...

  3. Android Xutils框架HttpUtil Get请求缓存问题

    话说,今天和服务器开发人员小小的逗逼了一下,为啥呢? 话说今天有个"收藏产品"的请求接口,是get request的哦,我客户端写好接口后,点击"收藏按钮",返 ...

  4. xUtils框架的使用

    xUtils简介 xUtils 包含了很多实用的android工具,xUtils 源于Afinal框架,对Afinal进行了大量重构,使得xUtils支持大文件上传,更全面的http请求协议支持,拥有 ...

  5. Android Xutils 框架(转)

    Android Xutils 框架 (转) 目录(?)[-] xUtils简介 目前xUtils主要有四大模块 使用xUtils快速开发框架需要有以下权限 混淆时注意事项 DbUtils使用方法 Vi ...

  6. (转) 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ

    特别棒的一篇文章,仍不住转一下,留着以后需要时阅读 基于Theano的深度学习(Deep Learning)框架Keras学习随笔-01-FAQ

  7. jfinal框架教程-学习笔记

    jfinal框架教程-学习笔记 JFinal  是基于 Java  语言的极速  WEB  + ORM  开发框架,其核心设计目标是开发迅速.代码量少.学习简单.功能强大.轻量级.易扩展.Restfu ...

  8. Android使用XUtils框架上传照片(一张或多张)和文本,server接收照片和文字(无乱码)

    Android上传图片,这里我使用了如今比較流行的XUtils框架.该框架能够实现文件上传.文件下载.图片缓存等等,有待研究. 以下是Android端上传的代码: xUtils.jar下载 Strin ...

  9. 【STM32H7教程】第12章 STM32H7的HAL库框架设计学习

    完整教程下载地址:http://forum.armfly.com/forum.php?mod=viewthread&tid=86980 第12章       STM32H7的HAL库框架设计学 ...

随机推荐

  1. 1064. Complete Binary Search Tree

    二叉排序树: http://www.patest.cn/contests/pat-a-practise/1064 #include <iostream> #include <vect ...

  2. ASP.NET中POST数据并跳转页面

    需求:先Post提交数据,然后跳转到目标页面 找了好久才发现这个神奇的类HttpHelper.原理很简单,利用html的from表单拼接,然后执行 使用方法: NameValueCollection ...

  3. Maven使用本地jar包(小私服?支持自动打入war包)

    1.库目录结构 D:\maven-local-repo\cn\xcf007\MD5\1.0\MD5-1.0.jar 2.安装到该本地库 mvn install:install-file -Dfile= ...

  4. ASP.NET MVC 简易在线书店

    写这篇博客的目的是为了记录自己的思想,有时候做项目做着做着就不知道下面该做什么了,把项目的具体流程记录下来,培养好习惯. 创建MVC项目 创建控制器StoreController public cla ...

  5. Indri查询命令及Java调用并保存结果

    查询参数 index Indri索引库路径.在参数文件中像/path/to/repository这样指定,在命令行中像-index=/path/to/repository这样指定.该参数可以设置多次来 ...

  6. [转]popwindow用法

    [转]弹出窗口的两种实现方式 PopupWindow 和 Activity  链接:http://www.cnblogs.com/winxiang/archive/2012/11/20/2778729 ...

  7. 使用Yeoman搭建 AngularJS 应用 (8) —— 让我们搭建一个网页应用

    原文地址:http://yeoman.io/codelab/write-app.html 创建一个新的模板来显示一个todo的列表 打开views/main.html 为了从一个干净的模板开始,删除m ...

  8. [Windows Azure] Querying Tables and Entities

    Constructing Filter Strings When constructing a filter string, keep these rules in mind: Use the log ...

  9. SQL 去除重复、获取最新记录

    应用中常会有需要去除重复的记录,或者获取某些最新记录(如:每个用户可以答题多次,每次答题时间不同,现在要获取所有用户的最新答题记录,即每个用户取最新的一条) 使用group 和max 即可实现上述功能 ...

  10. 极客范:如何使用 Cloud Insight 来监控闭路电视?

    最近新上线支持 Windows 系统及其组件 监控功能的 Cloud Insight,在系统监控领域基本囊括了对所有主流和部分非主流平台的支持.但是这还不够,Cloud Insight 可不仅仅是一个 ...