标签:
完全注解方式就可以进行UI绑定和事件绑定。
无需findViewById和setClickListener等。
1.view注解
// xUtils的view注解要求必须提供id,以使代码混淆不受影响。
@ViewInject
(
R
.
id
.
textView
)
TextView textView;
//@ViewInject(vale=R.id.textView, parentId=R.id.parentView)
//TextView textView;
@ResInject(id = R.string.label, type = ResType.String)
private String label;
2.监听的注解
// 取消了之前使用方法名绑定事件的方式,使用id绑定不受混淆影响
// 支持绑定多个id
@OnClick({R.id.id1, R.id.id2, R.id.id3})
// or @OnClick(value={R.id.id1, R.id.id2, R.id.id3}, parentId={R.id.pid1, R.id.pid2, R.id.pid3})
// 更多事件支持参见ViewCommonEventListener类和包com.lidroid.xutils.view.annotation.event。
@OnClick(R.id.test_button)
public void testButtonClick(View v){
// 方法签名必须和接口中的要求一致
}
注解完后,必须在activity或者fragment中注入view与事件
3.注入view与事件
//在Activity中注入:
@Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ViewUtils.inject(this);//注入view和事件...
textView.setText("some text...");
}
//在Fragment中注入:
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.bitmap_fragment, container,false);// 加载fragment布局
ViewUtils.inject(this, view);//注入view和事件
}
//在PreferenceFragment中注入:
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
ViewUtils.inject(this, getPreferenceScreen());//注入view和事件
}
// 其他重载
// inject(View view);
// inject(Activity activity)
// inject(PreferenceActivity preferenceActivity)
// inject(Object handler, View view)
// inject(Object handler, Activity activity)
// inject(Object handler, PreferenceGroup preferenceGroup)
// inject(Object handler, PreferenceActivity preferenceActivity)
|
//RequestParams params = new RequestParams();
// 默认编码UTF-8
//params.addHeader("name", "value"); //params.addQueryStringParameter("name", "value"); HttpUtils http = new HttpUtils(); http.configCurrentHttpCacheExpiry(1000 * 10); http.send(HttpRequest.HttpMethod.GET, " http://www.baidu.com", //params, new RequestCallBack<String>() { @Override public void onStart() { resultText.setText("conn..."); } @Override public void onLoading(long total, long current, boolean isUploading) { resultText.setText(current + "/" + total); } @Override public void onSuccess(ResponseInfo<String> responseInfo) { resultText.setText("response:" + responseInfo.result); } @Override public void onFailure(HttpException error, String msg) { resultText.setText(msg); } }); |
// 设置请求参数的编码
//RequestParams params = new RequestParams("GBK"); RequestParams params = new RequestParams(); // 默认编码UTF-8 //params.addQueryStringParameter("qmsg", "你好");
// 添加数据
//params.addBodyParameter("msg", "测试"); // 添加文件 params.addBodyParameter("file", new File("/sdcard/test.zip"));
// 继续添加文件
//params.addBodyParameter("testfile", new File("/sdcard/test2.zip"));
// 用于非multipart表单的单文件上传
//params.setBodyEntity(new FileUploadEntity(new File("/sdcard/test.zip"), "binary/octet-stream")); // 用于非multipart表单的流上传 //params.setBodyEntity(new InputStreamUploadEntity(stream ,length)); HttpUtils http = new HttpUtils(); // 设置返回文本的编码, 默认编码UTF-8 //http.configResponseTextCharset("GBK"); // 自动管理 cookie http.configCookieStore(preferencesCookieStore); http.send(HttpRequest.HttpMethod.POST,
"http://192.168.1.5:8080/UploadServlet",
params,
new RequestCallBack<String>() {
@Override
public void onStart() {
//开始
resultText.setText("conn...");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
//进度
if (isUploading) {
resultText.setText("upload: " + current + "/" + total);
} else {
resultText.setText("reply: " + current + "/" + total);
}
}
@Override
public void onSuccess(ResponseInfo<String> responseInfo) {
//成功 返回的结果:responseInfo.result
resultText.setText("reply: " + responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
//失败 错误信息:msg
resultText.setText(msg);
}
});
|
HttpUtils http = new HttpUtils();
HttpHandler handler = http.download(
"http://apache.dataguru.cn/httpcomponents/httpclient/source/httpcomponents-client-4.2.5-src.zip",
"/sdcard/httpcomponents-client-4.2.5-src.zip",
true,
//boolean autoResume:如果目标文件存在,接着未完成的部分继续下载。服务器不支持RANGE时将从新下载。
true,
//boolean autoRename:如果从请求返回信息中获取到文件名,下载完成后自动重命名。
new RequestCallBack<File>() {
@Override
public void onStart() {
// 开始
resultText.setText("conn...");
}
@Override
public void onLoading(long total, long current, boolean isUploading) {
// 进度
if (isUploading) {
resultText.setText("upload: " + current + "/" + total);
} else {
resultText.setText("reply: " + current + "/" + total);
}
}
@Override
public void onSuccess(ResponseInfo<File> responseInfo) {
// 成功 返回的结果:responseInfo.result
resultText.setText("reply: " + responseInfo.result);
}
@Override
public void onFailure(HttpException error, String msg) {
// 失败 错误信息:msg
resultText.setText(msg);
}
// 调用cancel()方法停止下载
handler.cancel();
|
//BitmapUtils可以说单例的 ,页可以根据需要重载多个获取实例
BitmapUtils bitmapUtils =newBitmapUtils(this);//
//设置默认加载图片
bitmapUtils.configDefaultLoadingImage(R.drawable.ic_launcher);
//设置失败后加载的图片
bitmapUtils.configDefaultLoadFailedImage(R.drawable.bitmap);
//设置Bitmap参数
bitmapUtils.configDefaultBitmapConfig(Bitmap.Config.RGB_565);
//内存缓存是否可用
//bitmapUtils.configMemoryCacheEnabled(false);
//硬盘缓存是否可用
//bitmapUtils.configDiskCacheEnabled(false);
//
//bitmapUtils.configDefaultAutoRotation(true);
//尺寸缩放动画
//ScaleAnimation animation = new ScaleAnimation(0.0f, 1.0f, 0.0f, 1.0f,
// Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f);
//animation.setDuration(800);
//设置默认图片加载动画
//bitmapUtils.configDefaultImageLoadAnimation(animation);
// 设置最大宽高, 不设置时更具控件属性自适应.
bitmapUtils.configDefaultBitmapMaxSize(BitmapCommonUtils.getScreenSize(getActivity()).scaleDown(3));
// 滑动时加载图片,快速滑动时不加载图片
//imageListView.setOnScrollListener(new PauseOnScrollListener(bitmapUtils, false, true));
//加载网络图片
bitmapUtils.display(testImageView,"http://bbs.lidroid.com/static/image/common/logo.png");
// 加载本地图片(路径以/开头, 绝对路径)
bitmapUtils.display(testImageView,"/sdcard/test.jpg");
// 加载assets中的图片(路径以assets开头)
bitmapUtils.display(testImageView,"assets/img/wallpaper.jpg");
// 使用ListView等容器展示图片时可通过PauseOnScrollListener控制滑动和快速滑动过程中时候暂停加载图片
listView.setOnScrollListener(newPauseOnScrollListener(bitmapUtils,false,true));
listView.setOnScrollListener(newPauseOnScrollListener(bitmapUtils,false,true, customListener));
|
package com.example.dbdemo;
import java.util.Date;
import com.lidroid.xutils.db.annotation.Column;
import com.lidroid.xutils.db.annotation.Finder;
import com.lidroid.xutils.db.annotation.Id;
import com.lidroid.xutils.db.annotation.Table;
import com.lidroid.xutils.db.annotation.Transient;
import com.lidroid.xutils.db.sqlite.FinderLazyLoader;
@Table(name = "person")// 建议加上注解, 混淆后表名不受影响
public class Person {
//必须有的属性
@Id
private int id;
@Column(column = "name")
private String name;
@Column(column = "email")
private String email;
@Column(column = "isAdmin")
private boolean isAdmin;
@Column(column = "time")
private Date time;
// Transient使这个列被忽略,不存入数据库
@Transient
private String willIgnore;
// 静态字段也不会存入数据库
private static String staticFieldWillIgnore;
@Finder(valueColumn = "id", targetColumn = "parentId")
public FinderLazyLoader<Child> children;
// 关联对象多时建议使用这种方式,延迟加载效率较高。
// @Finder(valueColumn = "id",targetColumn = "parentId")
// public Child children;
// @Finder(valueColumn = "id", targetColumn = "parentId")
// private List<Child> children;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getWillIgnore() {
return willIgnore;
}
public void setWillIgnore(String willIgnore) {
this.willIgnore = willIgnore;
}
public boolean isAdmin() {
return isAdmin;
}
public void setAdmin(boolean isAdmin) {
this.isAdmin = isAdmin;
}
public Date getTime() {
return time;
}
public void setTime(Date time) {
this.time = time;
}
}
|
// DbUtils 的创建有多个create方法,根据各自需求
DbUtils dbUtils = DbUtils.create(this, "demo.db");
dbUtils.configAllowTransaction(true);
dbUtils.configDebug(true);
Person p= new Person();
p.setEmail("3333");
p.setName("makk");
p.setAdmin(true);
p.setTime(new Date());
Child child = new Child();
child.name = "child‘ name";
try {
dbUtils.save(p);
// 保存对象关联数据库生成的id
// dbUtils.saveBindingId(p);
//其他操作
//删除
dbUtils.delete(p);
//更新
dbUtils.update(p, "name","email");
//查找
dbUtils.findAll(Selector.from(Person.class).where("id", "in", new int[] { 1, 3, 6 }));
} catch (DbException e) {
e.printStackTrace();
}
|
// 自动添加TAG,格式: className.methodName(L:lineNumber)
// 可设置全局的LogUtils.allowD = false,LogUtils.allowI = false...,控制是否输出log。
// 自定义log输出LogUtils.customLogger = new xxxLogger();
LogUtils.d("wyouflf");
|
标签:
原文地址:http://my.oschina.net/sfshine/blog/516988