xUtil3一共分为view、数据库、网络和图片加载四个模块。
首先下载jar,并且导入as,添加到库
然后写一个类集成application,并且在清单文件中配置权限等等。
public class BaseApplication extends Application { @Override public void onCreate() { super.onCreate(); x.Ext.init(this); }}写布局文件,几乎全是按钮,线性垂直分布,最后一个显示图片的控件。
最后上主程序代码,各部分都有简单示例。
public class MainActivity extends AppCompatActivity { @ViewInject(R.id.img_icon) ImageView imgIcon; PRivate DbManager dbManager = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ButterKnife.bind(this);//ButterKnifeioc x.view().inject(this);//xUtils createDB(); } private void createDB() { //创建数据库 DbManager.DaoConfig config = new DbManager.DaoConfig() .setDbName("xutils_db") .setDbDir(Environment.getExternalStorageDirectory()) .setDbVersion(1); dbManager = x.getDb(config); } @OnClick({R.id.btn_db_save, R.id.btn_db_get, R.id.btn_db_get_first, R.id.btn_db_update, R.id.btn_db_delete, R.id.btn_http_utils, R.id.btn_image}) public void onClick(View view) { switch (view.getId()) { case R.id.btn_db_save: dbSave(); break; case R.id.btn_db_get: getAll(); break; case R.id.btn_db_get_first: findFirst(); break; case R.id.btn_db_update: update(); break; case R.id.btn_db_delete: delete(); break; case R.id.btn_http_utils: httpRequest(); break; case R.id.btn_image: loadImage(); break; } } /** * 加载图片 */ private void loadImage() { //请求地址 final String imgUrl = "http://img1.imgtn.bdimg.com/it/u=1794894692,1423685501&fm=23&gp=0.jpg"; //请求选项 ImageOptions options = new ImageOptions.Builder() .setIgnoreGif(false)//忽略gif .setImageScaleType(ImageView.ScaleType.CENTER_CROP) .setLoadingDrawableId(R.mipmap.ic_launcher)//加载过程中显示的图片 .setFailureDrawableId(R.mipmap.ic_launcher)//加载失败后显示的图片 .build(); //回调接口 Callback.CommonCallback<Drawable> callback = new Callback.CommonCallback<Drawable>() { @Override public void onSuccess(Drawable drawable) { } @Override public void onError(Throwable throwable, boolean b) { } @Override public void onCancelled(CancelledException e) { } @Override public void onFinished() { } }; //请求加载 x.image().bind(imgIcon,imgUrl,options,callback); } /** * 网络请求 */ private void httpRequest() { //请求参数 RequestParams params = new RequestParams("https://www.baidu.com"); //回调接口 Callback.CommonCallback<String> callback = new Callback.CommonCallback<String>() { @Override public void onSuccess(String s) { Log.i("请求到的字符串", s); } @Override public void onError(Throwable throwable, boolean b) { } @Override public void onCancelled(CancelledException e) { } @Override public void onFinished() { } }; x.http().get(params, callback);//发出请求 } /** * 删 */ private void delete() { try { dbManager.delete(User.class, WhereBuilder.b("id", "=", 2)); Toast.makeText(this, "删除成功", Toast.LENGTH_SHORT).show(); } catch (DbException e) { e.printStackTrace(); } } /** * 改 */ private void update() { User user = new User(1, "willchan", 37);//修改id为1的数据 try { dbManager.update(user, "name", "age");//修改name和age Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show(); } catch (DbException e) { e.printStackTrace(); } } /** * 查一条,按条件查 */ private void findFirst() { try { //User user = dbManager.findFirst(User.class);//查第一条 //带条件 User user = dbManager.selector(User.class) .where("name", "=", "kalychen") .and("age", ">", 35) .findFirst(); Log.i("查询到的第一条数据", user.toString()); } catch (DbException e) { e.printStackTrace(); } } /** * 查所有 */ private void getAll() { try { List<User> users = dbManager.findAll(User.class); for (User user : users) { Log.i("查询到的数据", user.toString()); } } catch (DbException e) { e.printStackTrace(); } } /** * 增 */ private void dbSave() { //创建对象 User user = new User("kalychen", 38); try { //添加数据 dbManager.save(user); Toast.makeText(this, "数据写入成功", Toast.LENGTH_SHORT).show(); } catch (DbException e) { e.printStackTrace(); } }}View注入的部分很简单演示了一下,因为我们有ButterKnife.运行结果一切OK。
新闻热点
疑难解答