Xutils框架作用:
构建快速互联网开发中小型项目基石,gradle部署,一行代码解决项目中的网络请求,图片加载,本地缓存以及注解查询控件等功能,基于XUtils可实现咨询项目,电商项目,社区项目的快速开发上线。
注解的理解:
注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据),常见的作用有以下几种:
生成文档。这是最常见的,也是java 最早提供的注解。常用的有@see @param @return 等
跟踪代码依赖性,实现替代配置文件功能。比较常见的是spring 2.5 开始的基于注解配置。作用就是减少配置。现在的框架基本都使用了这种配置来减少配置文件的数量。也是
在编译时进行格式检查。如@override 放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出。
1、元注解
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
1.1、@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE)//注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS)// 默认的保留策略,注解会在class字节码文件中存在,但运行时无法得
@Retention(RetentionPolicy.RUNTIME)// 注解会在class字节码文件中存在,在运行时可以通过反射获取到
1.2、@Target:定义注解的作用目标
其定义的源码为:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字,由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等。
1.3、@Document:说明该注解将被包含在javadoc中
1.4、@Inherited:说明子类可以继承父类中的该注解
ViewUtils使用:
注解模块
https://blog.csdn.net/JKjiang123/article/details/78641629
@ContentView(R.layout.activity_view)关联xml
oncreate方法中x.view().inject(this);绑定activity
@ViewInject(R.id.ViewContentTv)初始化控件相当于findViewById
@Event(value = R.id.ViewClickBtn,
type = View.OnClickListener.class)
关联点击事件
自动安装apk
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result),“application/vnd.android.package-archive”);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DownLoadActivity.this.startActivity(intent);
自定义类继承Application
package com.example.day1_xutils;
import android.app.Application;
import org.xutils.x;
import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.SSLSession;
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(true);//允许debug运行
x.Ext.setDefaultHostnameVerifier(new HostnameVerifier() {
@Override
public boolean verify(String hostname, SSLSession session) {
return true;//忽略http校验
}
});
}
}
清单文件注册
一些具体操作
package com.example.day1_xutils;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
//查找视图的注解
@ContentView(R.layout.layout_view)
public class viewAction extends Activity {
//查找控件的注解
@ViewInject(R.id.but_dan)
Button but_dan;
@ViewInject(R.id.but_long)
Button but_long;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定视图
x.view().inject(this);
// but_dan.setOnClickListener(new View.OnClickListener() {
// @Override
// public void onClick(View v) {
//
// }
// });
// but_long.setOnLongClickListener(new View.OnLongClickListener() {
// @Override
// public boolean onLongClick(View v) {
// return true;
// }
// });
}
//点击事件的注解 一定要是私有,和原生一样
@Event(value = R.id.but_dan, type = View.OnClickListener.class)
private void onClick(View view) {
Toast.makeText(this, "你点击了", Toast.LENGTH_SHORT).show();
}
@Event(value = R.id.but_long, type = View.OnLongClickListener.class)
private boolean onLongClick(View view) {
Toast.makeText(this, "你长按了", Toast.LENGTH_SHORT).show();
return true;
}
}
数据库的应用
package com.example.day1_xutils;
import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.View;
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.x;
import java.util.List;
@ContentView(R.layout.layout_db)
public class dbAction extends Activity {
private DbManager.DaoConfig daoConfig;
private DbManager dbManager;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//绑定视图
x.view().inject(this);
//初始化数据库
daoConfig = new DbManager.DaoConfig();
//设置数据库名
daoConfig.setDbName("UserDao");
//设置版本号
daoConfig.setDbVersion(1);
//获取数据库管理者
dbManager = x.getDb(daoConfig);
}
@Event(value = {R.id.but_read,R.id.but_write},type = View.OnClickListener.class)
private void onClick(View v){
switch (v.getId()){
case R.id.but_read:
read();
break;
case R.id.but_write:
write();
break;
}
}
private void write() {
UserDao userDao = new UserDao();
userDao.setName("张新浩");
userDao.setAge(17);
userDao.setId(1);
UserDao userDao1 = new UserDao();
userDao1.setName("张新浩2");
userDao1.setAge(18);
userDao1.setId(2);
try {
//保存,多个要更新
dbManager.save(userDao);
dbManager.saveOrUpdate(userDao1);
} catch (DbException e) {
e.printStackTrace();
}
}
private void read() {
try {
//查找第一个
UserDao first = dbManager.selector(UserDao.class).findFirst();
Log.d("zxh",first.toString());
//按照id查找
UserDao byId = dbManager.findById(UserDao.class, 2);
Log.d("zxh2",byId.toString());
//查询所有
List<UserDao> all = dbManager.selector(UserDao.class).findAll();
} catch (DbException e) {
e.printStackTrace();
}
}
}
dao
package com.example.day1_xutils;
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
//表名的注解
@Table(name = "UserDao")
public class UserDao {
//列名的注解,是否自增
@Column(name = "id", isId = true)
private int id;
@Column(name = "name")
private String name;
@Column(name = "age")
private int age;
@Override
public String toString() {
return "UserDao{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}