1、添加现在依赖库(如果不成功,重启程序)
compile 'com.android.support:appcompat-v7:24.2.1'
自定义MyApplication
import org.xutils.x;//注意jar包一定不能导错
/**
* Created by Administrator on 2017/3/21.
*/
public class MyApplication extends Application {
@Override
public void onCreate() {
super.onCreate();
x.Ext.init(this);
x.Ext.setDebug(BuildConfig.DEBUG); // 是否输出debug日志, 开启debug会影响性能.
}
2、Manifest中添加属性
application
android:name=".MyApplication"3、MainAcvitity中的配置:
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
@ContentView(R.layout.activity_main)//加载布局
public class MainActivity extends AppCompatActivity {
@ViewInject(value = R.id.lv)//实例化对象
private ListView lv;
private String[] data = new String[]{"Image加载", "B", "C", "D"};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);//开启注解
setLvAdapter();
}
/**
* 点击按钮的监听
* @param view
*/
@Event(value = R.id.btn_parse, type = View.OnClickListener.class)
private void showToast(View view) {
Toast.makeText(this, "点击按钮", Toast.LENGTH_SHORT).show();
}
/**
* 展示ListView
*/
private void setLvAdapter() {
ArrayAdapter arrayAdapter = new ArrayAdapter(this, android.R.layout.simple_list_item_1, data);
lv.setAdapter(arrayAdapter);
}
/**
* 点击图片的跳转展示事件
* @param adapterView
* @param view
* @param i
* @param l
*/
@Event(value = R.id.lv, type = AdapterView.OnItemClickListener.class)
private void itemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent intent = new Intent(this, MainActivity.class);
switch (i) {
case 0:
intent.setClass(this, ImageActivity.class);
break;
}
startActivity(intent);
Toast.makeText(this, data[i], Toast.LENGTH_SHORT).show();
}
4、xutil中展示图片信息的方法
import org.xutils.common.util.DensityUtil;
import org.xutils.image.ImageOptions;
import org.xutils.x;
public class ImageActivity extends AppCompatActivity {
private ImageView iv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image);
iv = (ImageView) findViewById(R.id.iv);
ImageOptions imageOptions = new ImageOptions.Builder()
.setSize(DensityUtil.dip2px(120), DensityUtil.dip2px(120))
.setRadius(DensityUtil.dip2px(5))
// 如果ImageView的大小不是定义为wrap_content, 不要crop.
.setCrop(true) // 很多时候设置了合适的scaleType也不需要它.
// 加载中或错误图片的ScaleType
//.setPlaceholderScaleType(ImageView.ScaleType.MATRIX)
.setImageScaleType(ImageView.ScaleType.CENTER_CROP)
.setLoadingDrawableId(R.mipmap.ic_launcher)
.setFailureDrawableId(R.mipmap.ic_launcher)
.build();
x.image().bind(iv, "http://img.appd.lengxiaohua.cn/2016/05/24/a21ed7c5b5f1f_o.jpg", imageOptions);
}
}
5、xutil中网络请求的方法
import org.xutils.common.Callback;
import org.xutils.http.RequestParams;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
@ContentView(R.layout.activity_net)//加载布局文件
public class NetActivity extends AppCompatActivity {
@ViewInject(R.id.tv_result)
private TextView tv_result;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);//开启注解模式
}
@Event(value = R.id.btn_rusult,type = View.OnClickListener.class)
private void getResult(View view){
RequestParams params=new RequestParams("http://192.168.15.114:8080/HttpTest/Login");
params.addBodyParameter("username","zdsoft");
params.addBodyParameter("password","123456");
Callback.Cancelable cancelable=x.http().get(params, new Callback.CommonCallback<String>() {
@Override
public void onSuccess(String result) {
tv_result.setText(result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
tv_result.setText(ex.getMessage());
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
});
// cancelable.cancel();//取消请求
}
6、xutil中操作数据库DB的方法
新建实体类,通过注解的方式设置实体类的字段名以及字段约束
import org.xutils.db.annotation.Column;
import org.xutils.db.annotation.Table;
/**
* Created by Administrator on 2017/3/21.
*/
@Table(name = "user")
public class User {
@Column(name = "id", isId = true)
private int id;//该属性储存到哪个字段上
@Column(name = "password")
private String password;
@Column(name = "username")
private String username;
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
import org.xutils.DbManager;
import org.xutils.ex.DbException;
import org.xutils.view.annotation.ContentView;
import org.xutils.view.annotation.Event;
import org.xutils.view.annotation.ViewInject;
import org.xutils.x;
import java.util.List;
@ContentView(R.layout.activity_db)
public class DBActivity extends AppCompatActivity {
@ViewInject(value = R.id.et_user)
private EditText et_user;
@ViewInject(value = R.id.et_pwd)
private EditText et_pwd;
private DbManager.DaoConfig config;
@ViewInject(value = R.id.tv_content)
private TextView tv_content;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
x.view().inject(this);
//定义数据库对象,设置数据库名字和数据库版本号
config = new DbManager.DaoConfig().setDbName("test.db").setDbVersion(1);
}
/**
* 存数据的方法
*/
@Event(value = R.id.btn_save, type = View.OnClickListener.class)
private void save() {
User user = new User();
user.setUsername(et_user.getText().toString());
user.setPassword(et_pwd.getText().toString());
DbManager manager = x.getDb(config);
try {
manager.saveBindingId(user);//绑定ID保存数据
} catch (DbException e) {
e.printStackTrace();
}
quaryAll();
}
/**
* 查询数据的方法
*/
private void quaryAll() {
DbManager msg = x.getDb(config);
try {
List<User> list = msg.selector(User.class).findAll();//查询全部
for (User user : list) {
tv_content.append("id=" + user.getId() + " " +
"username=" + user.getUsername().toString() + " password=" + user.getPassword().toString());
}
} catch (DbException e) {
e.printStackTrace();
}
}