android-annotations使用入门

来源:互联网 发布:python 计算macd 编辑:程序博客网 时间:2024/05/21 06:26

转载请标明出处:http://blog.csdn.net/goldenfish1919/article/details/41577317

androidannotation是一个非常牛逼的框架(https://github.com/excilys/androidannotations/wiki),可以做到:依赖注入(Dependency Injection),简化的线程模型(Simplified  threading model),事件绑定(Event binding),REST Client。

非常好用,更重要的是它对性能无影响!本文我们简要的来看一下一些入门的东西。
1.从例子开始(参考https://github.com/excilys/androidannotations/wiki/FirstActivity):
AndroidManifest.xml
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     package="com.example.hello"  
  4.     android:versionCode="1"  
  5.     android:versionName="1.0" >  
  6.     <uses-sdk  
  7.         android:minSdkVersion="14"  
  8.         android:targetSdkVersion="18" />  
  9.     <application  
  10.         android:allowBackup="true"  
  11.         android:icon="@drawable/ic_launcher"  
  12.         android:label="@string/app_name">  
  13.         <activity  
  14.             android:name="com.example.hello.MainActivity_"  
  15.             android:label="@string/app_name" >  
  16.             <intent-filter>  
  17.                 <action android:name="android.intent.action.MAIN" />  
  18.                 <category android:name="android.intent.category.LAUNCHER" />  
  19.             </intent-filter>  
  20.         </activity>  
  21.         <activity android:name="com.example.hello.SecondActivity_" />  
  22.     </application>  
  23. </manifest>  
里面定义了两个activity,注意名字后面都带了一个下划线。

2.MainActivity.java:注意这里的名字没有下划线
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. @EActivity(R.layout.activity_main)  
  2. public class MainActivity extends Activity {  
  3.     @ViewById(R.id.myInput)  
  4.     EditText myInput;  
  5.     @ViewById(R.id.myTextView)  
  6.     TextView textView;  
  7.     @ViewById(R.id.myButton2)  
  8.     Button btn2;  
  9.     @StringRes(R.string.go_to_second)  
  10.     String btn2Txt;  
  11.     @AfterViews  
  12.     protected void afterViews(){  
  13.         btn2.setText(btn2Txt);  
  14.     }  
  15.     @Click  
  16.     void myButton() {  
  17.         String name = myInput.getText().toString();  
  18.         textView.setText("Hello " + name);  
  19.     }  
  20.     @Click  
  21.     void myButton2(){  
  22.         SecondActivity_.intent(this).start();  
  23.     }  
  24. }  
  25. activity_main.xml  
  26. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  27.     xmlns:tools="http://schemas.android.com/tools"  
  28.     android:layout_width="match_parent"  
  29.     android:layout_height="match_parent"  
  30.     android:orientation="vertical" >  
  31.     <EditText    
  32.         android:id="@+id/myInput"  
  33.         android:layout_width="fill_parent"   
  34.         android:layout_height="wrap_content"  />  
  35.     <Button    
  36.         android:id="@+id/myButton"  
  37.         android:layout_width="fill_parent"   
  38.         android:layout_height="wrap_content"   
  39.         android:text="Click me!" />          
  40.     <TextView    
  41.         android:id="@+id/myTextView"  
  42.         android:layout_width="fill_parent"   
  43.         android:layout_height="wrap_content" />      
  44.     <Button    
  45.         android:id="@+id/myButton2"  
  46.         android:layout_width="fill_parent"   
  47.         android:layout_height="wrap_content"   
  48.         android:text="@string/go_to_second"/>    
  49. </LinearLayout>  
SecondActivity的源码就不贴了。

使用注入以后,代码看上去变得很简洁,再也没有那一大堆findViewById之类的了。

如何进行编译运行呢(参考https://github.com/excilys/androidannotations/wiki/CustomizeAnnotationProcessing):
(1)如果是使用javac,需要传递-AandroidManifestFile=/path/to/AndroidManifest.xml这个参数,指明Manifest文件。
(2)如果是使用Eclipse,在项目上点击右键->Properties->Java Compiler->Annotation Processing->Enable annotation processing,
然后在Factory Path中添加AndroidAnnotations的jar包。
(3)如果是使用maven,maven-compiler-plugin的3.1版本可以设置编译参数。
[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <plugin>  
  2.     <artifactId>maven-compiler-plugin</artifactId>  
  3.     <version>3.1</version>  
  4.     <configuration>  
  5.         <encoding>UTF-8</encoding>  
  6.         <source>1.6</source>  
  7.         <target>1.6</target>  
  8.         <compilerArgs>  
  9.             <arg>-Atrace=true</arg>  
  10.             <arg>-AlogLevel=trace</arg>  
  11.             <arg>-AlogConsoleAppender=true</arg>  
  12.    <arg>-AandroidManifestFile=/path/to/AndroidManifest.xml</arg>  
  13.         </compilerArgs>  
  14.     </configuration>  
  15. </plugin>  
所有的可用的参数如下:
(1)trace:boolean,用来启用或者禁用@Trace注解,这个注解用会通过log记录方法的执行。
(2)androidManifestFile:string,默认情况下,AndroidAnnotations会在它的父文件夹中递归查找AndroidManifest.xml,假如你的工程的结构比较特殊,可以用这个进行指定。
(3)resourcePackageName:string,默认情况下,AndroidAnnotations会从AndroidManifest.xml文件中提取应用的package来找到R文件,假如R文件是在一个定制的package中,可以用它来设置。
(4)logFile:string,从3.0开始,AndroidAnnotations使用自定义的logger来在记录代码执行过程中的日志。日志默认会写在{outputFolder}/androidannotations.log文件中。
{outputFolder}参数按照下面的顺序进行查找:target-> build -> bin -> root project folder。如果找不到{outputFolder},可以使用logFile指定输出的文件。{outputFolder}可以使用占位符来动态改变文件名。
(5)logLevel:string,enum(trace, debug, info, warn, error):默认的日志级别是DEBUG,改为TRACE可能会有用。
(6)logAppenderConsole:boolean,默认情况下,AndroidAnnotations会使用FileAppender和MessagerAppender2个日志appender,FileAppender会写在日志文件中,MessagerAppender在IDE的消息列表中显示。可以设置logAppenderConsole为true来启用另一个ConsoleAppender。
(7)threadControl:boolean,用来启用或者禁用@SupposeUiThread和@SupposeBackground注解,默认是true,可以保证方法是在正确的线程中调用。

3.对性能的影响
无影响!因为它的原理是生成Activity类的子类,并不是使用反射!我们可以看下编译器帮我们自动生成的activity:
[java] view plaincopy在CODE上查看代码片派生到我的代码片
  1. public final class MainActivity_ extends MainActivity{  
  2.     @Override  
  3.     public void onCreate(Bundle savedInstanceState) {  
  4.         init_(savedInstanceState);//这里会处理@StringRes,@ColorRes等等  
  5.         super.onCreate(savedInstanceState);  
  6.         setContentView(layout.activity_main);//这里对应@EActivity(R.layout.activity_main)  
  7.     }  
  8.   
  9.   
  10.     private void init_(Bundle savedInstanceState) {  
  11.         Resources resources_ = this.getResources();  
  12.         btn2Txt = resources_.getString(string.go_to_second);  
  13.     }  
  14.   
  15.   
  16.     @Override  
  17.     public void setContentView(int layoutResID) {  
  18.         super.setContentView(layoutResID);  
  19.         afterSetContentView_();  
  20.     }  
  21.   
  22.   
  23.     @Override  
  24.     public void setContentView(View view, LayoutParams params) {  
  25.         super.setContentView(view, params);  
  26.         afterSetContentView_();  
  27.     }  
  28.   
  29.   
  30.     @Override  
  31.     public void setContentView(View view) {  
  32.         super.setContentView(view);  
  33.         afterSetContentView_();  
  34.     }  
  35.   
  36.   
  37.     private void afterSetContentView_() {  
  38. //@ViewById  
  39.         textView = ((TextView) findViewById(id.myTextView));  
  40.         myInput = ((EditText) findViewById(id.myInput));  
  41.         btn2 = ((Button) findViewById(id.myButton2));  
  42. //@Click  
  43.         {  
  44.             View view = findViewById(id.myButton2);  
  45.             if (view!= null) {  
  46.                 view.setOnClickListener(new OnClickListener() {  
  47.   
  48.   
  49.   
  50.   
  51.                     @Override  
  52.                     public void onClick(View view) {  
  53.                         MainActivity_.this.myButton2();  
  54.                     }  
  55.   
  56.   
  57.                 }  
  58.                 );  
  59.             }  
  60.         }  
  61. //@Click  
  62.         {  
  63.             View view = findViewById(id.myButton);  
  64.             if (view!= null) {  
  65.                 view.setOnClickListener(new OnClickListener() {  
  66.   
  67.   
  68.   
  69.   
  70.                     @Override  
  71.                     public void onClick(View view) {  
  72.                         MainActivity_.this.myButton();  
  73.                     }  
  74.   
  75.   
  76.                 }  
  77.                 );  
  78.             }  
  79.         }  
  80.         afterViews();//这里调用了@AfterViews标注的afterViews()方法  
  81.     }  
  82.     public static MainActivity_.IntentBuilder_ intent(Context context) {  
  83.         return new MainActivity_.IntentBuilder_(context);  
  84.     }  
  85.     public static class IntentBuilder_ {//这个是给startActivity和startActivityForResult用的  
  86.         private Context context_;  
  87.         private final Intent intent_;  
  88.   
  89.   
  90.         public IntentBuilder_(Context context) {  
  91.             context_ = context;  
  92.             intent_ = new Intent(context, MainActivity_.class);  
  93.         }  
  94.   
  95.   
  96.         public Intent get() {  
  97.             return intent_;  
  98.         }  
  99.   
  100.   
  101.         public MainActivity_.IntentBuilder_ flags(int flags) {  
  102.             intent_.setFlags(flags);  
  103.             return this;  
  104.         }  
  105.   
  106.   
  107.         public void start() {  
  108.             context_.startActivity(intent_);  
  109.         }  
  110.   
  111.   
  112.         public void startForResult(int requestCode) {  
  113.             if (context_ instanceof Activity) {  
  114.                 ((Activity) context_).startActivityForResult(intent_, requestCode);  
  115.             } else {  
  116.                 context_.startActivity(intent_);  
  117.             }  
  118.         }  
  119.     }  
  120. }  

最后看一下完整的pom.xml的例子(https://github.com/excilys/androidannotations/blob/develop/examples/maveneclipse/pom.xml):

[html] view plaincopy在CODE上查看代码片派生到我的代码片
  1. <?xml version="1.0" encoding="UTF-8"?>  
  2. <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd">  
  3.     <modelVersion>4.0.0</modelVersion>  
  4.     <groupId>org.androidannotations</groupId>  
  5.     <artifactId>maveneclipse</artifactId>  
  6.     <version>0.0.1-SNAPSHOT</version>  
  7.     <packaging>apk</packaging>  
  8.     <name>maveneclipse</name>  
  9.   
  10.     <properties>  
  11.         <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>  
  12.         <android.version>4.1.1.4</android.version>  
  13.         <android.platform>16</android.platform>  
  14.         <androidannotations.version>3.2</androidannotations.version>  
  15.         <java.version>1.6</java.version>  
  16.     </properties>  
  17.   
  18.     <dependencies>  
  19.         <dependency>  
  20.             <groupId>com.google.android</groupId>  
  21.             <artifactId>android</artifactId>  
  22.             <version>${android.version}</version>  
  23.             <scope>provided</scope>  
  24.         </dependency>  
  25.         <dependency>  
  26.             <groupId>org.androidannotations</groupId>  
  27.             <artifactId>androidannotations</artifactId>  
  28.             <version>${androidannotations.version}</version>  
  29.             <scope>provided</scope>  
  30.         </dependency>  
  31.         <dependency>  
  32.             <groupId>org.androidannotations</groupId>  
  33.             <artifactId>androidannotations-api</artifactId>  
  34.             <version>${androidannotations.version}</version>  
  35.         </dependency>  
  36.     </dependencies>  
  37.   
  38.     <build>  
  39.         <plugins>  
  40.             <plugin>  
  41.                 <artifactId>maven-compiler-plugin</artifactId>  
  42.                 <version>3.1</version>  
  43.                 <configuration>  
  44.                     <source>${java.version}</source>  
  45.                     <target>${java.version}</target>  
  46.                     <compilerArgs>  
  47.                         <arg>-AlogLevel=trace</arg>  
  48.                     </compilerArgs>  
  49.                 </configuration>  
  50.             </plugin>  
  51.             <plugin>  
  52.                 <groupId>com.jayway.maven.plugins.android.generation2</groupId>  
  53.                 <artifactId>android-maven-plugin</artifactId>  
  54.                 <version>3.9.0-rc.3</version>  
  55.                 <configuration>  
  56.                     <sdk>  
  57.                         <platform>${android.platform}</platform>  
  58.                     </sdk>  
  59.                     <undeployBeforeDeploy>true</undeployBeforeDeploy>  
  60.                 </configuration>  
  61.                 <extensions>true</extensions>  
  62.             </plugin>  
  63.         </plugins>  
  64.     </build>  
  65.   
  66. </project>  
0 0
原创粉丝点击