首页 > 代码库 > android-annotations使用入门
android-annotations使用入门
转载请标明出处:http://write.blog.csdn.net/postedit/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
- <?xml version="1.0" encoding="utf-8"?>
- <manifest xmlns:android="http://schemas.android.com/apk/res/android"
- package="com.example.hello"
- android:versionCode="1"
- android:versionName="1.0" >
- <uses-sdk
- android:minSdkVersion="14"
- android:targetSdkVersion="18" />
- <application
- android:allowBackup="true"
- android:icon="@drawable/ic_launcher"
- android:label="@string/app_name">
- <activity
- android:name="com.example.hello.MainActivity_"
- android:label="@string/app_name" >
- <intent-filter>
- <action android:name="android.intent.action.MAIN" />
- <category android:name="android.intent.category.LAUNCHER" />
- </intent-filter>
- </activity>
- <activity android:name="com.example.hello.SecondActivity_" />
- </application>
- </manifest>
2.MainActivity.java:注意这里的名字没有下划线
[java] view plaincopy
- @EActivity(R.layout.activity_main)
- public class MainActivity extends Activity {
- @ViewById(R.id.myInput)
- EditText myInput;
- @ViewById(R.id.myTextView)
- TextView textView;
- @ViewById(R.id.myButton2)
- Button btn2;
- @StringRes(R.string.go_to_second)
- String btn2Txt;
- @AfterViews
- protected void afterViews(){
- btn2.setText(btn2Txt);
- }
- @Click
- void myButton() {
- String name = myInput.getText().toString();
- textView.setText("Hello " + name);
- }
- @Click
- void myButton2(){
- SecondActivity_.intent(this).start();
- }
- }
- activity_main.xml
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical" >
- <EditText
- android:id="@+id/myInput"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/myButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Click me!" />
- <TextView
- android:id="@+id/myTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content" />
- <Button
- android:id="@+id/myButton2"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="@string/go_to_second"/>
- </LinearLayout>
使用注入以后,代码看上去变得很简洁,再也没有那一大堆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
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
- <configuration>
- <encoding>UTF-8</encoding>
- <source>1.6</source>
- <target>1.6</target>
- <compilerArgs>
- <arg>-Atrace=true</arg>
- <arg>-AlogLevel=trace</arg>
- <arg>-AlogConsoleAppender=true</arg>
- <arg>-AandroidManifestFile=/path/to/AndroidManifest.xml</arg>
- </compilerArgs>
- </configuration>
- </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
- public final class MainActivity_ extends MainActivity{
- @Override
- public void onCreate(Bundle savedInstanceState) {
- init_(savedInstanceState);//这里会处理@StringRes,@ColorRes等等
- super.onCreate(savedInstanceState);
- setContentView(layout.activity_main);//这里对应@EActivity(R.layout.activity_main)
- }
- private void init_(Bundle savedInstanceState) {
- Resources resources_ = this.getResources();
- btn2Txt = resources_.getString(string.go_to_second);
- }
- @Override
- public void setContentView(int layoutResID) {
- super.setContentView(layoutResID);
- afterSetContentView_();
- }
- @Override
- public void setContentView(View view, LayoutParams params) {
- super.setContentView(view, params);
- afterSetContentView_();
- }
- @Override
- public void setContentView(View view) {
- super.setContentView(view);
- afterSetContentView_();
- }
- private void afterSetContentView_() {
- //@ViewById
- textView = ((TextView) findViewById(id.myTextView));
- myInput = ((EditText) findViewById(id.myInput));
- btn2 = ((Button) findViewById(id.myButton2));
- //@Click
- {
- View view = findViewById(id.myButton2);
- if (view!= null) {
- view.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- MainActivity_.this.myButton2();
- }
- }
- );
- }
- }
- //@Click
- {
- View view = findViewById(id.myButton);
- if (view!= null) {
- view.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(View view) {
- MainActivity_.this.myButton();
- }
- }
- );
- }
- }
- afterViews();//这里调用了@AfterViews标注的afterViews()方法
- }
- public static MainActivity_.IntentBuilder_ intent(Context context) {
- return new MainActivity_.IntentBuilder_(context);
- }
- public static class IntentBuilder_ {//这个是给startActivity和startActivityForResult用的
- private Context context_;
- private final Intent intent_;
- public IntentBuilder_(Context context) {
- context_ = context;
- intent_ = new Intent(context, MainActivity_.class);
- }
- public Intent get() {
- return intent_;
- }
- public MainActivity_.IntentBuilder_ flags(int flags) {
- intent_.setFlags(flags);
- return this;
- }
- public void start() {
- context_.startActivity(intent_);
- }
- public void startForResult(int requestCode) {
- if (context_ instanceof Activity) {
- ((Activity) context_).startActivityForResult(intent_, requestCode);
- } else {
- context_.startActivity(intent_);
- }
- }
- }
- }
最后看一下完整的pom.xml的例子(https://github.com/excilys/androidannotations/blob/develop/examples/maveneclipse/pom.xml):
[html] view plaincopy
- <?xml version="1.0" encoding="UTF-8"?>
- <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">
- <modelVersion>4.0.0</modelVersion>
- <groupId>org.androidannotations</groupId>
- <artifactId>maveneclipse</artifactId>
- <version>0.0.1-SNAPSHOT</version>
- <packaging>apk</packaging>
- <name>maveneclipse</name>
- <properties>
- <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
- <android.version>4.1.1.4</android.version>
- <android.platform>16</android.platform>
- <androidannotations.version>3.2</androidannotations.version>
- <java.version>1.6</java.version>
- </properties>
- <dependencies>
- <dependency>
- <groupId>com.google.android</groupId>
- <artifactId>android</artifactId>
- <version>${android.version}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.androidannotations</groupId>
- <artifactId>androidannotations</artifactId>
- <version>${androidannotations.version}</version>
- <scope>provided</scope>
- </dependency>
- <dependency>
- <groupId>org.androidannotations</groupId>
- <artifactId>androidannotations-api</artifactId>
- <version>${androidannotations.version}</version>
- </dependency>
- </dependencies>
- <build>
- <plugins>
- <plugin>
- <artifactId>maven-compiler-plugin</artifactId>
- <version>3.1</version>
- <configuration>
- <source>${java.version}</source>
- <target>${java.version}</target>
- <compilerArgs>
- <arg>-AlogLevel=trace</arg>
- </compilerArgs>
- </configuration>
- </plugin>
- <plugin>
- <groupId>com.jayway.maven.plugins.android.generation2</groupId>
- <artifactId>android-maven-plugin</artifactId>
- <version>3.9.0-rc.3</version>
- <configuration>
- <sdk>
- <platform>${android.platform}</platform>
- </sdk>
- <undeployBeforeDeploy>true</undeployBeforeDeploy>
- </configuration>
- <extensions>true</extensions>
- </plugin>
- </plugins>
- </build>
- </project>
android-annotations使用入门
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。