注解(Annotation) 为我们在代码中添加信息提供了一种形式化的方法,是我们可以在稍后某个时刻方便地使用这些数据(通过 解析注解 来使用这些数据),常见的作用有以下几种:
• 生成文档。这是最常见的,也是java 最早提供的注解。常用的有@see @param @return 等
• 跟踪代码依赖性,实现替代配置文件功能。比较常见的是spring 2.5 开始的基于注解配置。作用就是减少配置。现在的框架基本都使用了这种配置来减少配置文件的数量。也是
• 在编译时进行格式检查。如@override 放在方法前,如果你这个方法并不是覆盖了超类方法,则编译时就能检查出。
元注解是指注解的注解。包括 @Retention @Target @Document @Inherited四种。
1、@Retention: 定义注解的保留策略
@Retention(RetentionPolicy.SOURCE)//注解仅存在于源码中,在class字节码文件中不包含
@Retention(RetentionPolicy.CLASS)// 默认的保留策略,注解会在class字节码文件中存在,但运行时无法得
@Retention(RetentionPolicy.RUNTIME)// 注解会在class字节码文件中存在,在运行时可以通过反射获取到
2、@Target:定义注解的作用目标
其定义的源码为:
@Documented
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.ANNOTATION_TYPE)
public @interface Target {
ElementType[] value();
}
@Target(ElementType.TYPE) //接口、类、枚举、注解
@Target(ElementType.FIELD) //字段、枚举的常量
@Target(ElementType.METHOD) //方法
@Target(ElementType.PARAMETER) //方法参数
@Target(ElementType.CONSTRUCTOR) //构造函数
@Target(ElementType.LOCAL_VARIABLE)//局部变量
@Target(ElementType.ANNOTATION_TYPE)//注解
@Target(ElementType.PACKAGE) ///包
其中的@interface是一个关键字,在设计annotations的时候必须把一个类型定义为@interface,而不能用class或interface关键字,由以上的源码可以知道,他的elementType 可以有多个,一个注解可以为类的,方法的,字段的等等。
3、@Document:说明该注解将被包含在javadoc中
4、@Inherited:说明子类可以继承父类中的该注解
ViewUtils使用:
方法命名一定要private 方法里面的参数,是谁的方法传入谁
注解模块
@ContentView(R.layout.activity_view)关联xml布局
oncreate方法中x.view().inject(this);绑定activity
@ViewInject(R.id.ViewContentTv)初始化控件相当于findViewById
@Event(value = R.id.ViewClickBtn,type = View.OnClickListener.class)关联点击事件
依赖
implementation 'org.xutils:xutils:3.5.1'
自定义Application
x.Ext.init(this);开启xutils
x.Ext.setDebug(true);
//方法命名一定要private 方法里面的参数,是谁的方法传入谁
@Event({R.id.btnClick})
private void getClick(View view){
Toast.makeText(this, "点击", Toast.LENGTH_SHORT).show();
}
@Event(value = R.id.btnLongClick,type = View.OnLongClickListener.class)
private boolean getlongClick(View view){
Toast.makeText(this, "长按点击", Toast.LENGTH_SHORT).show();
return true;
}
@Event(value = R.id.cb_check,type = CompoundButton.OnCheckedChangeListener.class)
private void getcheckBox(CompoundButton compoundButton,Boolean b){
if (b){
Toast.makeText(this, "选中了", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(this, "取消了", Toast.LENGTH_SHORT).show();
}
}
@ContentView(R.layout.activity_http)
public class HttpActivity extends AppCompatActivity {
@ViewInject(R.id.iv1)
ImageView imageView;
static final String str="http://www.qubaobei.com/ios/cf/dish_list.php?stage_id=1&limit=10&page=1";
static final String url="http://www.qubaobei.com/ios/cf/dish_list.php?";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_http);
x.view().inject(this);
}
@Event({R.id.b1,R.id.b2,R.id.b3})
private void gethttpget(View view){
switch (view.getId()){
case R.id.b1:
getData();
break;
case R.id.b2:
postData();
break;
case R.id.b3:
x.image().bind(imageView,"https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=1681359583,1850186605&fm=27&gp=0.jpg");
break;
default:
break;
}
}
private void postData() {
RequestParams requestParams = new RequestParams();
requestParams.setUri(url);
requestParams.addBodyParameter("stage_id","1");
requestParams.addBodyParameter("limit","10");
requestParams.addBodyParameter("page","2");
x.http().post(requestParams, new Callback.CacheCallback() {
@Override
public void onSuccess(String result) {
Toast.makeText(HttpActivity.this, result, Toast.LENGTH_SHORT).show();
Log.e("###post",result);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
}
@Override
public boolean onCache(String result) {
return false;
}
});
}
private void getData() {
//设置请求参数
RequestParams requestParams = new RequestParams(str);
x.http().get(requestParams, new Callback.CacheCallback() {
//请求成功
@Override
public void onSuccess(String result) {
Log.e("#####",result);
Toast.makeText(HttpActivity.this, "成功", Toast.LENGTH_SHORT).show();
}
//请求错误
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
//取消
@Override
public void onCancelled(CancelledException cex) {
}
//完成请求
@Override
public void onFinished() {
}
@Override
public boolean onCache(String result) {
return false;
}
});
}
}
@ContentView(R.layout.activity_down_load)
public class DownLoadActivity extends AppCompatActivity {
private static final String SAVERATH = Environment.getExternalStorageDirectory().getAbsolutePath()+"/qq.apk";
ProgressDialog progressDialog;
static final String str="http://softfile.3g.qq.com:8080/msoft/179/24659/43549/qq_hd_mini_1.4.apk";
@ViewInject(R.id.downLoad)
Button button;
Callback.Cancelable cancelable;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_down_load);
x.view().inject(this);
initProgressDialog();
}
//创建一个带进度条的dialog
private void initProgressDialog(){
progressDialog = new ProgressDialog(this);
progressDialog.setTitle("下载apk");
progressDialog.setMessage("下载中....");
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setButton(ProgressDialog.BUTTON_NEGATIVE, "暂停", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
cancelable.cancel();
}
});
}
@Event(R.id.downLoad)
private void getclick(View view){
download();
}
private void download() {
RequestParams requestParams = new RequestParams();
requestParams.setUri(str);
//设置可以断点续传
requestParams.setAutoRename(true);
requestParams.setCancelFast(true);
requestParams.setSaveFilePath(SAVERATH);
cancelable = x.http().get(requestParams, new Callback.ProgressCallback() {
@Override
public void onSuccess(File result) {
Toast.makeText(DownLoadActivity.this, "下载成功"+result.getAbsolutePath(), Toast.LENGTH_SHORT).show();
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(result),"application/vnd.android.package-archive");
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
DownLoadActivity.this.startActivity(intent);
}
@Override
public void onError(Throwable ex, boolean isOnCallback) {
}
@Override
public void onCancelled(CancelledException cex) {
}
@Override
public void onFinished() {
progressDialog.cancel();
}
@Override
public void onWaiting() {
}
@Override
public void onStarted() {
progressDialog.show();
}
@Override
public void onLoading(long total, long current, boolean isDownloading) {
progressDialog.setProgress((int) (current * 100 / total));
}
});
}
}
@ContentView(R.layout.activity_db)
public class DBActivity extends AppCompatActivity {
@ViewInject(R.id.tv1)
TextView textView;
List list = new ArrayList<>();
DbManager db;
FoodBean food;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_db);
x.view().inject(this);
downLoad();
DbManager.DaoConfig daoConfig = new DbManager.DaoConfig();
daoConfig.setDbName("x1702");
daoConfig.setDbVersion(1);
db = x.getDb(daoConfig);
}
@Event({R.id.insert,R.id.seacher})
private void get(View view){
switch (view.getId()){
case R.id.insert:
insert();
break;
case R.id.seacher:
seacher();
break;
}
}
private void seacher() {
try {
List all = db.findAll(Food.class);
StringBuilder sb = new StringBuilder();
for (Food f: all) {
sb.append(f).append("\n");
}
textView.setText(sb.toString());
Toast.makeText(this, sb.toString(), Toast.LENGTH_SHORT).show();
Log.e("#####",sb.toString());
} catch (DbException e) {
e.printStackTrace();
}
}
private void insert() {
for (int i=0;i() {
@Override
public void onSuccess(String result) {
try {
JSONObject jsonObject = new JSONObject(result);
JSONArray data = jsonObject.getJSONArray("data");
for (int i=0;i