Android开发框架xUtils简介(附有不使用findViewById的方法)

xUtils简介目前xUtils主要有四大模块:

1 . DbUtils模块:

android中的orm框架,一行代码就可以进行增删改查; 支持事务,默认关闭;

2 . ViewUtils模块:

3 . HttpUtils模块:

4 . BitmapUtils模块:

加载bitmap的时候无需考虑bitmap加载过程中出现的oom和android容器快速滑动时候出现的图片错位等现象;支持加载网络图片和本地图片; 内存管理使用lru算法,更好的管理bitmap内存;可配置线程加载线程数量,缓存大小,缓存路径,加载显示动画等…使用xUtils快速开发框架需要有以下权限: =”android.permission.WRITE_EXTERNAL_STORAGE” />混淆时注意事项:DbUtils使用方法(一般结合Gson使用):DbUtils db = DbUtils.create(this);User user = new User(); //这里需要注意的是User对象必须有id属性,或者有通过@ID注解的属性user.setEmail(“wyouflf@qq.com”);user.setName(“wyouflf”);db.save(user); // 使用saveBindingId保存实体时会为实体的id赋值…// 查找Parent entity = db.findById(Parent.class, parent.getId());List<Parent> list = db.findAll(Parent.class);//通过类型查找Parent Parent = db.findFirst(Selector.from(Parent.class).where(“name”,”=”,”test”));// IS NULLParent Parent = db.findFirst(Selector.from(Parent.class).where(“name”,”=”, null));// IS NOT NULLParent Parent = db.findFirst(Selector.from(Parent.class).where(“name”,”!=”, null));// WHERE id<54 AND (age>20 OR age<30) ORDER BY id LIMIT pageSize OFFSET pageOffsetList<Parent> list = db.findAll(Selector.from(Parent.class).where(“id” ,”<“, 54).and(WhereBuilder.b(“age”, “>”, 20).or(“age”, ” < “, 30)).orderBy(“id”).limit(pageSize).offset(pageSize * pageIndex));// op为”in”时,最后一个参数必须是数组或Iterable的实现类(例如List等)Parent test = db.findFirst(Selector.from(Parent.class).where(“id”, “in”, new int[]{1, 2, 3}));// op为”between”时,最后一个参数必须是数组或Iterable的实现类(例如List等)Parent test = db.findFirst(Selector.from(Parent.class).where(“id”, “between”, new String[]{“1”, “5”}));DbModel dbModel = db.findDbModelAll(Selector.from(Parent.class).select(“name”));//select(“name”)只取出name列List<DbModel> dbModels = db.findDbModelAll(Selector.from(Parent.class).groupBy(“name”).select(“name”, “count(name)”));…List<DbModel> dbModels = db.findDbModelAll(sql); // 自定义sql查询db.execNonQuery(sql) // 执行自定义sql…示例创建表的代码package com.test.dukang.mysqlite;import com.lidroid.xutils.db.annotation.Column;import com.lidroid.xutils.db.annotation.Table;/** * Created by Administrator on 2015/9/14. */@Table(name = “myuser”){@Column(column=”id”)private int id;private String name;private String password;() {return id;}(int id) {this.id = id;}public String getName() {return name;}(String name) {this.name = name;}public String getPassword() {return password;}(String password) {this.password = password;}}向表中添加数据 DbUtils db = DbUtils.create(this,”Data.db”);User user = new User(); //这里需要注意的是User对象必须有id属性,或者有通过@ID注解的属性user.setName(“wyouflf@qq.com”);user.setName(“wyouflf”);try {db.save(user);} catch (DbException e) {e.printStackTrace();}查询表中的数据 DbUtils db2 = DbUtils.create(this, “Data.db”);try {List<User> list = db2.findAll(User.class);//通过类型查找for (User usersigle: list){Log.d(“123”,usersigle.getName());}} catch (DbException e) {e.printStackTrace();}其他语句的使用参照上面的示例ViewUtils使用方法完全注解方式就可以进行UI绑定和事件绑定。无需findViewById和setClickListener等。// xUtils的view注解要求必须提供id,以使代码混淆不受影响。@ViewInject(R.id.textView)TextView textView;(id = R.string.label, type = ResType.String)private String label;(R.id.test_button)(View v) { // 方法签名必须和接口中的要求一致…}…(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.main);ViewUtils.inject(this); //注入view和事件…textView.setText(“some text…”);…}View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {View view = inflater.inflate(R.layout.bitmap_fragment, container, false); // 加载fragment布局ViewUtils.inject(this, view); //注入view和事件…}(Bundle savedInstanceState) {super.onActivityCreated(savedInstanceState);ViewUtils.inject(this, getPreferenceScreen()); //注入view和事件…}HttpUtils使用方法:普通的get()方法HttpUtils http = new HttpUtils();http.send(HttpRequest.HttpMethod.GET,”http://www.lidroid.com”,new RequestCallBack<String>(){(long total, long current, boolean isUploading) {testTextView.setText(current + “/” + total);}(ResponseInfo<String> responseInfo) {textView.setText(responseInfo.result);}() {}(HttpException error, String msg) {}});使用HttpUtils上传文件 或者 提交数据 到服务器(post方法)RequestParams params = new RequestParams();params.addHeader(“name”, “value”);params.addQueryStringParameter(“name”, “value”);// 只包含字符串参数时默认使用BodyParamsEntity,// 类似于UrlEncodedFormEntity(”application/x-www-form-urlencoded”)。params.addBodyParameter(“name”, “value”);params.addBodyParameter(“file”, new File(“path”));…HttpUtils http = new HttpUtils();http.send(HttpRequest.HttpMethod.POST,”uploadUrl….”,params,new RequestCallBack<String>() {() {testTextView.setText(“conn…”);}(long total, long current, boolean isUploading) {if (isUploading) {testTextView.setText(“upload: ” + current + “/” + total);} else {testTextView.setText(“reply: ” + current + “/” + total);}}(ResponseInfo<String> responseInfo) {testTextView.setText(“reply: ” + responseInfo.result);}(HttpException error, String msg) {testTextView.setText(error.getExceptionCode() + “:” + msg);}});使用HttpUtils下载文件:绊脚石乃是进身之阶。

Android开发框架xUtils简介(附有不使用findViewById的方法)

相关文章:

你感兴趣的文章:

标签云: