android 之XUitls

一  、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<View> callBack = new DefaultBitmapLoadCallBack<View>() {
@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<File>() {


@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<File> arg0) {
// 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<File>() {


@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<File> arg0) {
// 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<Object>() {


@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<Object> arg0) {
// TODO Auto-generated method stub
Toast.makeText(MainActivity.this,
"success:" + arg0.result, Toast.LENGTH_SHORT)
.show();
}


@Override
public void onLoading(long total, long current,
boolean isUploading) {
// TODO Auto-generated method stub
super.onLoading(total, current, isUploading);
}
});
}


四、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> list = db.findAll(Parent.class);//通過類型查找  
 
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<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)"));  
...  
//例:分組聚合查詢出  Parent表中 非重複的name和它的對應數量  
List<DbModel> dbModels = db.findDbModelAll(Selector.form(Parent.class).select("distinct name,count(name) as num").groupBy("name"));   
db.execNonQuery("sql") // 執行自定義sql  
...  


}











發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章