zoukankan      html  css  js  c++  java
  • Android Annotations浅析


    这阵子遇到了好多事情,挺久没来更新博文了,这两天在学这个开源框架Android Annotations,用起来感觉挺方便的,

    相信用过Spring注解的孩子理解起来应该比較easy!

    就是配置起来比較吃力。

    关于AndroidAnnotaions的介绍,网上已经非常多了,我这里不再累赘。

      1、AndroidAnnotations官网:http://androidannotations.org/(或许你须要翻墙)

      2、eclipse中使用androidannotations的配置方法说明:https://github.com/excilys/androidannotations/wiki/Eclipse-Project-Configuration

      3、Android Studio中配置AndroidAnnotations:(这个是我这篇博文中要涉及到的!


    一、Android Studio配置androidannotations环境。

    1、首先你建立一个module之后,在相应的app中会有一个名为build.gradle的文件(该module有效),而在整个项目外面也会有一个名为build.gradle的文件(全局有效)【这个工具中的application的文件夹下(相当于Eclipse下的workspace)是能够有多个module的(相当于Eclipse下的project)】

    2、我们配置的时候大概要分为以下两步

    在局部build.gradle中(增加红色字体部分):

    apply plugin: 'com.android.application'
    apply plugin: 'android-apt'
    def AAVersion = '3.0.1'



    android {
        compileSdkVersion 19
        buildToolsVersion "20.0.0"


        defaultConfig {
            applicationId "com.tongbu.mytest"
            minSdkVersion 8
            targetSdkVersion 19
            versionCode 1
            versionName "1.0"
        }
        buildTypes {
            release {
                runProguard false
                proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
            }
        }
    }


    dependencies {
        compile fileTree(dir: 'libs', include: ['*.jar'])
        compile 'com.android.support:appcompat-v7:19.+'

        apt "org.androidannotations:androidannotations:$AAVersion"
        compile "org.androidannotations:androidannotations-api:$AAVersion"

    }


    apt {
        arguments {
            androidManifestFile variant.processResources.manifestFile
            resourcePackageName 'com.tongbu.mytest'
        }
    }


    在全局build.gradle中(增加红色字体部分):

    buildscript {
        repositories {
            jcenter()
        }
        dependencies {
            classpath 'com.android.tools.build:gradle:0.12.+'
            classpath 'com.neenbedankt.gradle.plugins:android-apt:1.3'
        }
    }


    allprojects {
        repositories {
            jcenter()
        }
    }


    假设以上的都顺利完毕了。那么恭喜你已经快配置完了,还须要把AndroidManifest.xml中的Activity的名字后面加上 _(下划线),由于这个框架解析编译的时候,比方类MainActivity会被解析成MainActivity_.class。所以在清单文件里我们要在Activity的名字后面加一个下划线。或者androidannotation会报错!

    但并不会这么顺利。在你补充完下划线之后。你会发现会提示你找不到MainActivity_这个东东

    那么怎么办呢??我们说了它是在编译的时候整出来的。那我们仅仅要按一下编译的按钮就可以生成了!!



    这样子androidannotation在android studio上的环境就配置好了。Eclipse的话资料比較多。再这里就不介绍了


    二、一个Demo来了解androidannotations的部分注解

    @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"); } }



    几个方法的先后调用顺序:



    资源涉及的注解(不一一列举):


  • 相关阅读:
    高性能服务器开发基础系列 (二)Reactor模式
    高性能服务器开发基础系列 (二)Reactor模式
    高性能服务器开发基础系列 (二)Reactor模式
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    javax.net.ssl.SSLHandshakeException: Remote host closed connection during handshake
    JS基础入门篇(十)— 数组方法
    JS基础入门篇(十)— 数组方法
    C# WinForm 文件上传下载
  • 原文地址:https://www.cnblogs.com/clnchanpin/p/6781563.html
Copyright © 2011-2022 走看看