一 、ViewUtils
1、使用anotation进行布局绑定 在activity类名上/进行布局绑定@ContentView(R.layout.activity_main)
2、在activity的onCreate中
ViewUtils.inject(this);
// 日志utils
LogUtils.allowV = true;
3、在成员变量中可进行控件绑定和资源绑定
@ViewInject(R.id.image)
private ImageView imageView;
// 资源绑定
@ResInject(id = R.drawable.ic_launcher, type = ResType.Drawable)
private Drawable drawable
4、事件绑定 事件绑定的方法参数,返回类型必须和相应的Listerner中要实现的方法一致,可以支持多个控件,支持事件类型20余种
// 事件绑定
@OnClick({ R.id.button_bitmap_utils, R.id.button_db_utils_create_table,
R.id.button_db_utils_update, R.id.button_db_utils_insert,
R.id.button_db_utils_update, R.id.button_db_utils_delete,
R.id.button_http_utils, R.id.button_view_utils,
R.id.button_http_utils_download, R.id.button_http_utils_upload })
public void onClickListener(View v) {
switch (v.getId()) {
case R.id.button_bitmap_utils:
testBitmapUtils();
break;
case R.id.button_http_utils:
testHttpUtils();
break;
case R.id.button_http_utils_download:
testHttpUtilsDownload();
break;
case R.id.button_http_utils_upload:
testHttpUtilsUpload();
break;
case R.id.button_view_utils:
Toast.makeText(this, "button_view_utils", Toast.LENGTH_SHORT)
.show();
break;
case R.id.button_db_utils_create_table:
testDbUtilsCreateTable();
break;
case R.id.button_db_utils_update_table:
break;
case R.id.button_db_utils_insert:
break;
case R.id.button_db_utils_update:
break;
case R.id.button_db_utils_delete:
break;
}
}
二、BitmapUtils
/**
* BitmapUtils加载网络图片
*/
private void testBitmapUtils() {
String url = "http://p4.music.126.net/LwrtLOyNOIcFjIz7SmRY4w==/6066005650778514.jpg";
/** 注意,若不使用BitmapDisplayConfig则用BitmapUtils设置默认图片既可以 */
BitmapUtils bitmapUtils = new BitmapUtils(this);
bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
bitmapUtils.configDefaultLoadFailedImage(R.drawable.ic_launcher);
bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
/** 注意,若要使用BitmapDisplayConfig则上吗设置的默认图片不起作用,应该使用config设置默认图片 */
BitmapDisplayConfig config = new BitmapDisplayConfig();
config.setBitmapConfig(Bitmap.Config.RGB_565);
config.setBitmapMaxSize(BitmapCommonUtils.getScreenSize(this));
config.setLoadFailedDrawable(drawable);
config.setLoadingDrawable(drawable);
// 注意实例化类为DefaultBitmapLoadCallBack
BitmapLoadCallBack
@Override
public void onLoadStarted(View container, String uri,
BitmapDisplayConfig config) {
// TODO Auto-generated method stub
super.onLoadStarted(container, uri, config);
}
@Override
public void onLoading(View container, String uri,
BitmapDisplayConfig config, long total, long current) {
// TODO Auto-generated method stub
super.onLoading(container, uri, config, total, current);
}
@Override
public void onLoadCompleted(View container, String uri,
Bitmap bitmap, BitmapDisplayConfig config,
BitmapLoadFrom from) {
// TODO Auto-generated method stub
super.onLoadCompleted(container, uri, bitmap, config, from);
Toast.makeText(MainActivity.this, "加载完成", Toast.LENGTH_SHORT)
.show();
}
@Override
public void onLoadFailed(View container, String uri,
Drawable drawable) {
// TODO Auto-generated method stub
super.onLoadFailed(container, uri, drawable);
}
};
// 不使用BitmapDisplayConfig和回调
bitmapUtils.display(imageView, url);
// 使用BitmapDisplayConfig和回调
// bitmapUtils.display(imageView, url, config, callBack);
}
三、HttpUtils
/**
* 测试HttpUtils文件上传 POST
*/
private void testHttpUtilsUpload() {
String url = "http://image.baidu.com/albumlist/1761607680%20608895455";
HttpUtils httpUtils = new HttpUtils();
RequestParams params = new RequestParams();
File dirFile = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
params.addBodyParameter("file", new File(dirFile, "xiaoqiang.jpg"));
httpUtils.send(HttpMethod.POST, url, params,
new RequestCallBack
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"failure:" + arg0.getMessage(),
Toast.LENGTH_LONG).show();
}
@Override
public void onLoading(long total, long current,
boolean isUploading) {
// TODO Auto-generated method stub
super.onLoading(total, current, isUploading);
if (isUploading) {
LogUtils.v("正在上传:" + total + "/" + current);
}
}
@Override
public void onSuccess(ResponseInfo
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "succeess",
Toast.LENGTH_LONG).show();
}
});
}
/**
* 下载文件方法
*
* @param method
* 请求用get还是post等
* @param url
* 下载文件的url
* @param target
* 下载保存的目录
* @param params
* 参数
* @param autoResume
* 是否自动恢复下载
* @param autoRename
* 是否自动重命名
* @param callback
* 回调
*/
private void testHttpUtilsDownload() {
String url = "http://ww1.sinaimg.cn/crop.0.0.800.800.1024/735510dbjw8eoo1nn6h22j20m80m8t9t.jpg";
HttpUtils httpUtils = new HttpUtils();
File dirFile = Environment
.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES);
String target = new File(dirFile, "xiaoqiang.jpg").getAbsolutePath();
httpUtils.download(url, target, true, true,
new RequestCallBack
@Override
public void onFailure(HttpException arg0, String arg1) {
// TODO Auto-generated method stub
String eroMsg = arg0.getMessage();
Toast.makeText(MainActivity.this, "failure:" + eroMsg,
Toast.LENGTH_SHORT).show();
}
@Override
public void onSuccess(ResponseInfo
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this, "success",
Toast.LENGTH_SHORT).show();
}
@Override
public void onLoading(long total, long current,
boolean isUploading) {
// TODO Auto-generated method stub
super.onLoading(total, current, isUploading);
LogUtils.v("正在下载 " + total + "/" + current);
}
});
}
/**
* 测试HttpUtils网络访问请求
*/
private void testHttpUtils() {
String url = "https://www.baidu.com/";
HttpUtils httpUtils = new HttpUtils();
RequestParams params = new RequestParams();
params.addBodyParameter("pno", "1");
params.addBodyParameter("psize", "10");
httpUtils.send(HttpMethod.POST, url, params,
new RequestCallBack
四、DbUtils
/**
* 测试创建数据库和table
* @throws DbException
*/
private void testDbUtils() {
DbUtils db = DbUtils.create(this);
User user = new User(); //这里需要注意的是User对象必须有id属性,或者有通过@ID注解的属性
user.setEmail("[email protected]");
user.setName("wyouflf");
db.save(user); // 使用saveBindingId保存实体时会为实体的id赋值
...
// 查找
Parent entity = db.findById(Parent.class, parent.getId());
List
Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","=","test"));
// IS NULL
Parent Parent = db.findFirst(Selector.from(Parent.class).where("name","=", null));
// IS NOT NULL
Parent 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 pageOffset
List
.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
...
//例:分组聚合查询出 Parent表中 非重复的name和它的对应数量
List
db.execNonQuery("sql") // 执行自定义sql
...
}