android-annotations使用入門
來源:程序員人生 發(fā)布時(shí)間:2014-12-15 08:39:41 閱讀次數(shù):2540次
轉(zhuǎn)載請(qǐng)標(biāo)明出處:http://write.blog.csdn.net/postedit/41577317
androidannotation是1個(gè)非常牛逼的框架(https://github.com/excilys/androidannotations/wiki),可以做到:依賴注入(Dependency Injection),簡化的線程模型(Simplified threading model),事件綁定(Event binding),REST Client。
非常好用,更重要的是它對(duì)性能無影響!本文我們扼要的來看1下1些入門的東西。
1.從例子開始(參考https://github.com/excilys/androidannotations/wiki/FirstActivity
):
AndroidManifest.xml
-
<?xml version="1.0" encoding="utf⑻"?>
-
<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>
里面定義了兩個(gè)activity,注意名字后面都帶了1個(gè)下劃線。
2.MainActivity.java:注意這里的名字沒有下劃線
-
@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>
SecondActivity的源碼就不貼了。
使用注入以后,代碼看上去變得很簡潔,再也沒有那1大堆findViewById之類的了。
如何進(jìn)行編譯運(yùn)行呢(參考https://github.com/excilys/androidannotations/wiki/CustomizeAnnotationProcessing):
(1)如果是使用javac,需要傳遞-AandroidManifestFile=/path/to/AndroidManifest.xml這個(gè)參數(shù),指明Manifest文件。
(2)如果是使用Eclipse,在項(xiàng)目上點(diǎn)擊右鍵->Properties->Java Compiler->Annotation Processing->Enable annotation processing,
然后在Factory Path中添加AndroidAnnotations的jar包。
(3)如果是使用maven,maven-compiler-plugin的3.1版本可以設(shè)置編譯參數(shù)。
-
<plugin>
-
<artifactId>maven-compiler-plugin</artifactId>
-
<version>3.1</version>
-
<configuration>
-
生活不易,碼農(nóng)辛苦
如果您覺得本網(wǎng)站對(duì)您的學(xué)習(xí)有所幫助,可以手機(jī)掃描二維碼進(jìn)行捐贈(zèng)