Androidannotations框架

Java注解:

   注解(Annotation),也叫元数据。一种代码级别的说明。它是JDK1.5及以后版本引入的一个特性,与类、接口、枚举是在同一个层次。它可以声明在包、类、字段、方法、局部变量、方法参数等的前面,用来对这些元素进行说明,注释。

作用分类:
①编写文档:通过代码里标识的元数据生成文档【生成文档doc文档】
② 代码分析:通过代码里标识的元数据对代码进行分析【使用反射】
③编译检查:通过代码里标识的元数据让编译器能够实现基本的编译检查【Override】
Androidannotations框架特性
1、使用依赖注入(Dependency Injection)

2、简化的线程模型(Simplified  threading model)  

3、事件绑定(Event binding)

4、REST Client

5、No Magic  [不知道为什么这样称呼,直译过来就是:无魔法,它的意思是:AndroidAnnotations在编译

的时候会产生一个子类(接下来你会明白),你查看这个子类,可以看到它是如何工作的]

AndroidAnnotations官网: http://androidannotations.org/

代码:

@NoTitle  //无标题
@Fullscreen //全屏
@EActivity(R.layout.activity_my)
public class MyActivity extends ActionBarActivity {
    //==============================================基本的注解=================================================
    @ViewById
    Button button1;

    @ViewById
    Button button2;

    @ViewById(R.id.textview1)   //指定id的注入
    TextView textView;

    @ViewById
    ProgressBar progressBar;

    @ViewById
    ImageView imageView;

    //获取系统service的方法(取代原来的clipboardManager = (ClipboardManager) getSystemService(CLIPBOARD_SERVICE);)
    @SystemService
    ClipboardManager clipboardManager;

    @Click({R.id.button1,R.id.button2,R.id.button3,R.id.button4})
    public void simpleButtonOnClicked(View view){
        switch (view.getId()){
            case R.id.button1:{
                textView.setText("Button1 is Clicked!");
            }
            break;
            case R.id.button2:{
                textView.setText("Button2 is Clicked!");
            }
            break;
            case R.id.button3:{
                String content = clipboardManager.getText().toString();
                Toast.makeText(getApplicationContext(),"剪贴板内容: " + content, Toast.LENGTH_SHORT).show();
            }
            break;
            case R.id.button4:{
                Toast.makeText(getApplicationContext(),"滚动条开始了!",Toast.LENGTH_SHORT).show();
                progressBarWorks();
            }
            break;
        }
    }


    @LongClick({R.id.button2})
    public void buttonOnLongClicked(View view){
        switch (view.getId()){
            case R.id.button1:{
                textView.setText("Button1 is LongClicked!");//由于没注册,所以不可能被触发
            }
            break;
            case R.id.button2:{
                textView.setText("Button2 is LongClicked!");//可触发
            }
            break;
        }
    }

    //===================================================关于资源的注解=========================================

    @AnimationRes(R.anim.rotate)
    Animation animationRotate;

    @DrawableRes(R.drawable.myphoto)
    Drawable myphoto;

    @ColorRes(R.color.love)
    Integer mycolor;

    @TextRes(R.string.textres)
    CharSequence text;

    @Click({R.id.button5,R.id.button6,R.id.button7})
    public void animationButtonOnClicked(View view){
        switch (view.getId()){
            case R.id.button5:{
                imageView.startAnimation(animationRotate);
            }
            break;
            case R.id.button6:{
                imageView.setImageDrawable(myphoto);
            }
            break;
            case R.id.button7:{
                Toast.makeText(getApplicationContext(),text.toString(),Toast.LENGTH_SHORT).show();
            }
            break;
        }
    }

    //==============================================关于线程的注解================================================
    //相当于一个新的任务AsyncTask或者新线程Thread
    @Background
    public void progressBarWorks(){
        //相当于一个新的线程中执行: @Background
        int i = 1;
        while (i <= 10){
            Log.e("progress","进度: " + i);
            try {
                Thread.sleep(1000);
                updateProgressBar(i);
                //直接progressBar.setProgress(i);也可以的,所以@Background注解内部可能实现了handler机制
                i++;
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
        }

    }

    //指代UI线程
    @UiThread
    public void updateProgressBar(int i){
        progressBar.setProgress(i);
        if (i == 10){
            Toast.makeText(getApplicationContext(), "滚动条结束",Toast.LENGTH_SHORT).show();
        }
    }

    //=======================================关于几个事件的先后顺序===============================================

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.e("FirstToLast", "onCreate");

        //可省略!
        //setContentView(R.layout.activity_my);

        //progressBar.setMax(100);  报错,空指针异常
        //因为在onCreate()被调用的时候,@ViewById还没有被set,也就是都为null
        //所以如果你要对组件进行一定的初始化,那么你要用@AfterViews注解
    }

    @AfterViews
    public void init(){
        Log.e("FirstToLast","init");
        progressBar.setMax(10);
    }

    @Override
    protected void onResume() {
        super.onResume();
        Log.e("FirstToLast","onResume");
    }

    @Override
    protected void onStart() {
        super.onStart();
        Log.e("FirstToLast","onStart");
    }


}
原文地址:https://www.cnblogs.com/yoyohong/p/6140366.html