关于androidannotations(注解)的理解和使用(文档篇)
本文內容來自對github上提供的文檔的翻譯,由于本人英文水平有限,可能會有很多不當之處,還請各位大神予以指教和批評。
androidannotations是由 Pierre-Yves Ricau創建的,文檔說明由Kay-Uwe Janssen編寫。
github地址:
- 項目地址:https://github.com/excilys/androidannotations
- 文檔介紹:https://github.com/excilys/androidannotations/wiki
- 官網網址:http://androidannotations.org/
目標
通過依賴注入的方式實現:
- View、資源的初始化
- 事件綁定
- 服務注冊
- 簡單的線程模型,通過annotation表示方法運行在UI線程還是后臺線程。
優點
- 在編譯時生成子類,對性能沒有影響,并且我們能夠check the code 來查看它是如何工作的。
- 項目大小只有50kb。
- 有效的簡化代碼,避免重復繁瑣工作的進行。
- 使我們的代碼更加便于維護。
androidannotations是如何工作的
androidannotations使用 Java Annotation Processing Tool標準動態添加一個額外的資源目錄,用來存放生成的子類。
Activity注解
例如:當我們使用@EActivity 注解activity時,會在同一個包下的另一個資源目錄中生成一個在名稱末尾添加了“_”的同名子類。下面的例子清楚的顯示了這一特性。
//使用@EActivity注解MyActivity package com.some.company; @EActivity public class MyActivity extends Activity {// ... }//注解后生成的子類,會在原activity名稱后添加一個"_" package com.some.company; public final class MyActivity_ extends MyActivity {// ... }這個生成的子類會在注解的activity中添加一些重寫的方法,如oncreate()等,并將這些方法的調用權限授權給原activity。正是因為這個原因,我們在清單文件中注冊activity時,名稱后面千萬不能忘記在末尾加上”_”.
使用@EActivity注解進行布局綁定
@EActivity(R.layout.main) public class MyActivity extends Activity {}
@EActivity的參數必須是有效的布局id,表示此布局將被作為the Content View在activity中使用,相當于我們經常使用的setContentView(R.layout.main)。代碼示例如下:可以不給此注解提供參數,這時表示我們將會采用傳統的方式,即在onCreate方法中調用setContentView方法進行布局綁定。代碼示例如下:
利用annotated啟動一個activity
通常情況下,我們start一個activity時,會調用startActivity(this,MyListActivity.class),這時啟動的activity參數是MyListActivity,但是使用AndroidAnnotations時,我們需要調用的activity參數應該是MyListActivity_,即startActivity(this,MyListActivity_.class)。Intent綁定啟動activity
從AndroidAnntations2.4開始,提供了一個啟動注解生成activity的靜態幫助類,通過它我們可以:通過綁定intent來啟動activity;
//// Starting the activity MyListActivity_.intent(context).start();綁定一個到activity的intent
// Building an intent from the activity Intent intent = MyListActivity_.intent(context).get();在綁定intent時,定義flags
// You can provide flags MyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();定義通過intent傳送的數據
// You can even provide extras defined with @Extra in the activity MyListActivity_.intent(context).myDateExtra(someDate).start();
從AndroidAnntations2.7開始,我們可以使用startActivityForResult()方法來啟動activity了。
MyListActivity_.intent(context).startForResult(REQUEST_CODE);使用@OnActivityResult注解來獲取結果:
@OnActivityResult(REQUEST_CODE) void onResult(int resultCode) { }@OnActivityResult的參數必須是integer常量,這個常量是啟動activity時提供的請求碼。onResult方法可以有多個參數:
- 攜帶返回數據的android.content.Intent
- int或Integer的resultCode。
下面是幾個@OnActivityResult的示例:
@OnActivityResult(REQUEST_CODE)void onResult(int resultCode, Intent data) {}@OnActivityResult(REQUEST_CODE)void onResult(int resultCode) {}@OnActivityResult(ANOTHER_REQUEST_CODE)void onResult(Intent data) {}@OnActivityResult(ANOTHER_REQUEST_CODE)void onResult() {}從AndroidAnntations3.2開始,可以使用@OnActivityResult.Extra向帶有返回參數的intent注入extra。這個注解只能作為參數使用,并要保證使用此參數的方法已經和 @OnActivityResult注解進行綁定了。參數的類型必須是可添加的Bundle,其value就是結果data的key。如果沒有設置,則使用變量名作為key。下面是幾個例子:
@OnActivityResult(REQUEST_CODE) void onResult(int resultCode, Intent data, @OnActivityResult.Extra String value) { }@OnActivityResult(REQUEST_CODE) void onResult(int resultCode, @OnActivityResult.Extra(value = "key") String value) { }@OnActivityResult(REQUEST_CODE) void onResult(@OnActivityResult.Extra String strVal, @OnActivityResult.Extra int intVal) { }
MyListActivity_.intent(context).withOptions(bundle).start();
從AndroidAnntations3.3開始,我們可以通過綁定intent來輕松的傳遞Bundle了。從AndroidAnntations4.0開始,可以在綁定intent時,添加過渡動畫了!
MyListActivity_.intent(context).start().withAnimation(enterAnimRes, exitAnimRes));
Fragment注解
使用注解操作Fragments
@EActivity(R.id.main) public class DetailsActivity extends FragmentActivity {}
在AndroidAnnotation2.6之前,并沒有fragment使用的注解支持,然而我們確信通過繼承FragmentActivity取代繼承Activity并 不會破壞AndroidAnnotations。就像這樣:2.6之后AndroidAnnotations對android.app.Fragment和android.support.v4.app.Fragment都提供了支持,并能動態識別fragment的類型來使用正確的API。
@EFragment public class MyFragment extends Fragment {}
對fragment而言,需要使用@EFragment注解。示例如下:使用注解后,同樣會生成一個以”“結尾的子類,如MyFragment。當創建一個新的fragment時,需要在布局文件中使用生成的子類,像這樣:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="fill_parent"android:layout_height="fill_parent"android:orientation="horizontal" ><fragment android:id="@+id/myFragment"android:name="com.company.MyFragment_"android:layout_width="fill_parent"android:layout_height="fill_parent" /></LinearLayout>Java代碼中這樣創建新實例:
MyFragment fragment = new MyFragment_();或使用builder:
MyFragment fragment = MyFragment_.builder().build();在fragment中,我們可以使用所有的注解:
@EFragment public class MyFragment extends Fragment {@BeanSomeBean someBean;@ViewByIdTextView myTextView;@AppMyApplication customApplication;@SystemServiceActivityManager activityManager;@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)UserDao userDao;@Clickvoid myButton() {}@UiThreadvoid uiThread() {}@AfterInjectvoid calledAfterInjection() {}@AfterViewsvoid calledAfterViewInjection() {}@Receiver(actions = "org.androidannotations.ACTION_1")protected void onAction1() {} }
View injection and event listener binding will only be based on viewscontained inside the fragment. Note, however, that it’s isn’t currently the case for @EBean injected inside fragments: they haveaccess to the activity views.
上面這段話摘自github上提供的文檔說明,沒有看明白,如果有了解這方面的大神,可以在評論中告訴我,我將不勝感激!^_^
- 使用注解給fragment綁定布局
很簡單,只需要給@EFragment添加參數即可。這個參數就是要綁定布局的ID。
@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
}
如果我們需要重寫onCreateView()方法時,比如需要用到savedInstanceState時,我們仍然可以使用這個注解,只需要將方法的返回值設置為null即可。 - 強制布局注入
從AndroidAnnotation3.3開始,如果使用注解的類中onCreateView方法返回null,或其父類的onCreateView方法返回null而子類沒用重寫父類的該方法,我們就只是把要注入的布局當做第一個注解參數。這時因為我們不想重載onCreateView方法返回的空布局。在某些情況下,下面這些行為是可以預期的:例如,一個類繼承自ListFragment,它的onCreateView方法返回為null,并且布局沒有通過@EFragment注解注入。為了在這種情況下實現功能,我們需要添加一個boolean類型的forceLayoutInjection參數。如果設置為true,布局注入將忽視onCreateView方法的返回值。,但是這個參數默認情況下為false,所以如果沒有明確的指出forceLayoutInjection為true,布局將會被該方法的返回值替代(It is false by default, so the old behavior takes place if you do not explicitly set forceLayoutInjection to true.)。 注入Fragments
可以使用 @EActivity, @EFragment, @EView, @EViewGroup, @EBean, using @FragmentById or @FragmentByTag注解標簽將fragment注入到類中,如果沒有特殊給定值,將在注解中使用變量名。- 使用@FragmentById or @FragmentByTag只能將fragment注入,不能創建fragment。所以我們要保證fragment已經存在activity中(布局文件中定義或者oncreate方法中創建)。
- 即使fragment沒有使用@EFragment注解標注,我們也可以將這些fragment注入到activity中。
@EActivity(R.layout.fragments) public class MyFragmentActivity extends FragmentActivity {@FragmentByIdMyFragment myFragment;@FragmentById(R.id.myFragment)MyFragment myFragment2;@FragmentByTagMyFragment myFragmentTag;@FragmentByTag("myFragmentTag")MyFragment myFragmentTag2; }
相關方法
@EActivity(R.layout.fragments) public class MyFragmentActivity extends FragmentActivity {@FragmentByIdvoid setOneFragmentById(MyFragment myFragmentId){// do something with myFragmentId}void setMultipleFragmentsById(@FragmentById MyFragment myFragmentId, @FragmentById(R.id.myFragment) MyFragment myFragmentId2){// do something with myFragmentId and myFragmentId2}@FragmentByTagvoid setOneFragmentByTag(MyFragment myFragmentTag){// do something with myFragmentTag}void setMultipleFragmentsByTag(@FragmentByTag MyFragment myFragmentTag, @FragmentByTag("myFragmentTag") MyFragment myFragmentTag2){// do something with myFragmentTag and myFragmentTag2} }
從AndroidAnnotation4.0開始,出現的基于注解的方法:使用注解時,DialogFragment要注意:
很不幸,在使用注解@EFragment時,我們不能通過onCreateDialog()方法創建新的Dialog實例。我們只能調用super.onCreateDialog(),設置其返回的Dialog實例。然后我們可以在由@AfterViews注解的方法中來自定義views。Fragment Argument
從AndroidAnnotations 2.7開始,可以使用@FragmentArg注解標簽對Fragment綁定對應的參數。
在生成的builder中,setter方法總是使用字段名稱作為參數名稱。默認情況下,綁定參數值的關鍵就是字段名稱,不過我們可以通過給標簽提供一個值來改變這一點。
- 相關方法
從 AndroidAnnotations 4.0開始:
Fragment 構造器擁有對這些參數的專用方法:
MyFragment myFragment = MyFragment_.builder().myMessage("Hello").anotherStringArgument("World").build();自定義類的注解(如實體類等)
我們可以使用注解@EBean標注非Android標準組件(如Activity、service)的類。注意:這個@EBean要求被標注的類只用一個構造函數,并且這個構造函數不能有參數(或者,向AndroidAnnotations 2.7中一樣,只能擁有一個Context類型的參數)。
@EBean public class MyClass {}- 在已經使用注解的類中或Android組件中引用另外一個被注解的類只需要使用@Bean注解即可
如果這個被標注類不是單例的話,使用@Bean注解后,我們將會獲得它的實例。
與@Bean相關的方法
@EActivity public class MyActivity extends Activity {@Beanvoid setOneBean(MyClass myClass){// do something with myClass}void setMultipleBeans(@Bean MyClass myClass, @Bean MyOtherClass myOtherClass){// do something with myClass and myOtherClass} }值得注意的是,生成的子類都是final類,也就意味著生成的子類是不可被繼承的。然而,我們可以繼承原始類,并且原始類中生成的代碼也將為子類使用(However, you can extends original classes and generated code will be also generated for the child class. )。這適用于所有的注解。
@EActivity public class MyChildActivity extends MyActivity {}
例如:下面MyChildActivity也會有myOtherClass的注入。注入接口的實現類到定義的接口字段中
@EActivity public class MyActivity extends Activity {/* A MyImplementation instance will be injected. * MyImplementation must be annotated with @EBean and implement MyInterface.*/@Bean(MyImplementation.class)MyInterface myInterface;}
從Since AndroidAnnotations 2.5開始,如果想要根據Since AndroidAnnotations 的API使用依賴關系,比如實現接口。我們可以通過@Bean的參數來實現。只需要將實現類作為@Bean的參數傳進去即可,但是要保證實現類由@EBean標注了并且實現了該接口。雖然這樣做以后,類仍然知道其以來的實現,但是至少使用這些依賴的代碼必須根據API實現。
@EBean支持的注解
@EBean public class MyClass {@SystemServiceNotificationManager notificationManager;@UiThreadvoid updateUI() {}}
在@EBean標注的類中可以使用幾乎全部的AA 注解。我們可以在@EBean標注的類中使用和view相關的標注(如@View,@Click… …)。
@EBean public class MyClass {@ViewByIdTextView myTextView;@Click(R.id.myButton)void handleButtonClick() {} }注意:只有在依賴MyClass類的根Android組件(Activity等)包含需要的views,上面的代碼才能有效。否則myTextView將會是null,handleButtonClick方法將永遠不會被調用。
@EBean public class MyClass {//1.@RootContext能取到調用該Bean的context,構造方法不用傳context@RootContextContext context;//2.得到的是調用Activity的context對象@RootContextActivity activity;// 3.得到的是調用service的Context對象@RootContextService service;// 4.得到的是調用MyActivity的context對象@RootContextMyActivity myActivity;}
使用@RootContext可以獲取到Bean的context對象,如下:- 與RootContext相關的方法
從AndroidAnnotations 4.0開始:
依賴注入后執行代碼過程
@EActivity public class MyActivity extends Activity {@BeanMyClass myClass;//MyClass類可以看上文中的代碼 }
當@EBean標注的類的構造方法被調用時,其字段還沒有被注入,所以下面代碼中的MyClass類的service字段將會是null。如果想要在build時執行代碼,在依賴注入后我們需要使用@AfterInject注解標注一些方法:
@EBean public class MyClass {@SystemServiceNotificationManager notificationManager;@BeanMyOtherClass dependency;public MyClass() {// notificationManager and dependency are null}//標注后service將會被注入@AfterInjectpublic void doSomethingAfterInjection() {// notificationManager and dependency are set} }警告:
父類和子類中不能出現由@AfterViews, @AfterInject or @AfterExtras標注的同名方法,否則會出現錯誤。如github有人提出這樣的問題(https://github.com/excilys/androidannotations/issues/591):
如圖,當把Test2中的init()方法名變更一下,就可以運行正確了。
- 當我們調用這些注解標注的方法時有固定的順序,但是對于由相同注解@AfterXXX標注的方法在調用時是沒有固定順序的。如github上有人提出這樣的問題(https://github.com/excilys/androidannotations/issues/810):
@AfterXXX調用順序
- 使用相同的@AfterXXX標注的方法的調用順序
同一個類中使用相同@AfterXXX標注的方法的調用順序是無法保證的。如果我們想要按照一定的順序調用多個方法,應該創建一個被標注的方法,然后按照一定的順序使用這個方法去調用其他的方法。如果要處理父/子類中使用相同@AfterXXX標注的方法,我們可以在https://github.com/excilys/androidannotations/issues/810#issuecomment-31218529找到答案。
當 @AfterExtras標注的方法被調用時,所有沒和view關聯的注入都會被完成。因此使用@Bean標注的字段是很安全的方式。這些views只會在@AfterViews標注的方法調用時才會被獲取。
每當intent到達Activity時,@AfterExtras 標注的方法都會被調用。
對于Activity而言,每次執行setContentView()方法時,@AfterViews被標注的方法都會被調用。- 使用相同的@AfterXXX標注的方法的調用順序
Extras
使用注解@Extra標注的字段,表示Activity的字段需要連同開啟此Activity的intent傳來的相應Extra一起被注入。
示例:
@EActivity public class MyActivity extends Activity {//myStringExtra和字段myMessage一起被注入@Extra("myStringExtra")String myMessage;//myDateExtra和字段myDateExtraWithDefaultValue 一起被注入@Extra("myDateExtra")Date myDateExtraWithDefaultValue = new Date();}從AndroidAnnotations 2.6開始,如果沒有為@Extra添加參數,則直接使用字段名作為extra的名稱。如:
@EActivity public class MyActivity extends Activity {// The name of the extra will be "myMessage"@ExtraString myMessage; }- 相關方法
從AndroidAnnotations 4.0開始:
我們可以使用前面提到的Intent綁定啟動activity方法來傳遞extra values。
onNewIntent()方法
@EActivity public class MyActivity extends Activity {@Extra("myStringExtra")String myMessage;@Overrideprotected void onNewIntent(Intent intent) {super.onNewIntent(intent);//傳遞intent時自動注入extrassetIntent(intent);} }
從AndroidAnnotations 2.6開始,AndroidAnnotations重寫了setIntent方法,并會在調用該方法時自動注入指定intent傳遞的extras。這就有便于我們在onNewIntent方法中調用setIntent方法時自動注入extras。從AndroidAnnotations 3.2開始,如果在Activity中有被 @AfterExtras標注的方法,我們就可以重寫onNewIntent方法來調用setIntent方法。
警告:
從AndroidAnnotations 4.0開始,我們不再重載onNewIntent方法,如果有需要的話,只能自己手動重載該方法了。extras注入后執行的代碼
@EActivity public class MyClass {@ExtraString someExtra;@Extraint anotherExtra;@AfterExtraspublic void doSomethingAfterExtrasInjection() {// someExtra and anotherExtra are set to the value contained in the incoming intent// if an intent does not contain one of the extra values the field remains unchanged} }
如果在注入extras之后需要進行操作的話,我們需要將執行邏輯方法標注上@AfterExtras。如:
Views注入
@ViewById代表Activity的字段和布局中的view進行了綁定,相當于調用了findViewById方法。可以把布局中相應view的id可以放在注解參數的位置進行指定,如@ViewById(R.id.myTextView)。如果沒有給注解添加參數,就會使用字段的名字。字段的訪問權限千萬不能設置為私有。
示例:
- 相關方法
從AndroidAnnotation4.0開始:
- @ViewsById
從AndroidAnnotations 3.1開始使用的注解標簽。與@ViewById相似,只是此標簽用來注入一系列的view。會在java.util.List or android.view.View的子類字段上進行標注。它的參數值是一組R.id.*的值。注入完成之后,除去為null的view之外(為了避免增加null檢查的代碼),這些view的ID會被保存在list集合中。
- 相關方法
從AndroidAnnotations 4.0開始:
- @AfterViews
@AfterViews表示被標注的方法會在view綁定后被調用。
onCreate方法被調用時,@ViewById還沒有被執行,因此,我們只能在@AfterViews標注的方法中添加view綁定之后要執行的邏輯。
我們可以用@AfterViews標注多個方法,但是一定要記得,我們不可以在onCreate方法中使用任何和view綁定的字段,否則會拋出空指針異常。
@EActivity(R.layout.main) public class MyActivity extends Activity {@ViewByIdTextView myTextView;@Overridepublic void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);// DON'T DO THIS !! It will throw a NullPointerException, myTextView is not set yet.// myTextView.setText("Date: " + new Date());}注入總是盡快進行的,因此我們可以很放心的使用任何被注解標注的字段,如@AfterView標注的方法中被@Extra或者@InstanceState標注的字段,因為這些注解不要求任何view的注入。所以我們可以很安全地假設@AfterViews標注的方法中這些字段已經被它們的預期值初始化了。
@EActivity(R.layout.main) public class MyActivity extends Activity {@ViewByIdTextView myTextView;@InstanceStateInteger textPosition;@AfterViewsvoid updateTextPosition() {myTextView.setSelection(textPosition); //Sets the cursor position of myTextView}自定義view注解
從事一段時間的開發以后,我們會發現一件事情:我們經常會在不同的位置用到相同的布局,從而會不停的調用相同的方法來控制這些布局。自定義view會幫我們從這些繁瑣的工作中解放出來。
創建自定義組件時,我們會用到@EView 和@EViewGroup 。
- @EView
從AndroidAnnotations 2.4開始:
創建一個繼承View的類并用@EView進行標注。這樣,我們就可以在這個類中使用注解進行邏輯操作了。如:
在布局文件中使用自定義的view,名稱上不要忘記要加上“_”:
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent"android:orientation="vertical" ><-- 名稱上記得加"_"--><com.androidannotations.view.CustomButton_ android:layout_width="match_parent"android:layout_height="wrap_content" /> </LinearLayout>當然,也可以通過代碼動態創建,只是名稱上也要添加”_”:
CustomButton button = CustomButton_.build(context);- 自定義ViewGroup注解
從AndroidAnnotations 2.2開始,我們可以使用@EViewGroup自定義ViewGroup。
1.創建要用到的布局文件:
使用merge標簽可以:在布局文件被引用時,子布局會直接被添加到父布局上去,我們可以在view樹上看到。
上述代碼中使用了很多relativeLayout特有的屬性,是因為這個布局文件我打算放到RelativeLayout中去。
下面我們在代碼中創建自定義的view:
如何使用自定義的view呢?
很簡單,我們可以像引用其他控件一樣在布局文件中找到位置帶包名引用即可,要注意的是,類名稱一定是帶“_”的。
在Activity中聲明并使用自定義控件:
@EActivity(R.layout.main) public class Main extends Activity {@ViewByIdprotected TitleWithSubtitle firstTitle, secondTitle, thirdTitle;@AfterViewsprotected void init() {firstTitle.setTexts("decouple your code","Hide the component logic from the code using it.");secondTitle.setTexts("write once, reuse anywhere","Declare you component in multiple " +"places, just as easily as you " +"would put a single basic View.");thirdTitle.setTexts("Let's get stated!","Let's see how AndroidAnnotations can make it easier!");} }同樣的,很多其他的注解標簽在自定義控件中也是可以使用的。
AdapterViewEvents
在AdapterView中,我們可以使用以下注解來綁定方法去處理事件:
- @ItemClick:點擊事件
- @ItemLongClick:長點擊事件
- @ItemSelect:條目選擇事件
這些注解的值是R.id.*,可以是多個。如果沒有給定值,那么會把方法的名稱當做值的名稱。
被@ItemClick或@ItemLongClick標注的方法必須擁有一個參數,這個參數可以是任意類型,當調用adapter.getItem(position)時,會使用這個參數。
被@ItemSelect標注的方法可能會有一個或兩個參數。第一個參數必須是boolean類型,第二個參數是從adapter被選擇的位置傳來的object類型。
如:
從AndroidAnnotations 2.4開始,如果被這些注解標注的方法的參數給定的是int類型,則說明從adapter傳來的不是對象,而是該對象所在的位置,或者說是被點擊的位置。
Resource注解
所有的@xxxRes標簽代表Activity中的字段要和res文件夾下的相對應的資源綁定了。res的ID同樣是作為@xxxRes的參數進行傳遞的,如果沒有給定參數,那么將使用字段的名稱作為id的名稱。
- @StringRes
用來和string資源進行綁定。如:
- @ColorRes
用來綁定顏色資源。
- @AnimationRes
用來綁定XmlResourceParser字段(不太常用)或Animation字段(常用)。
- @DimensionRes
用來檢索dimension資源。
- @DimensionPixelOffsetRes
用來檢索dimension資源,會將檢索到的dimension的小數部分全部截去,保留整型值,除此之外與@DimensionRes是一樣的。
- @DimensionPixelSizeRes
用來檢索dimension資源,會將檢索到的dimension四舍五入保留整型值,除此之外與@DimensionRes是一樣的。
- 其他的資源注解標簽
- @BooleanRes
- @ColorStateListRes
- @DrawableRes
- @IntArrayRes
- @IntegerRes
- @LayoutRes
- @MovieRes
- @TextRes
- @TextArrayRes
- @StringArrayRes
- 相關方法
從AndroidAnnotations 4.0開始:這兩個方法適用于上面提到的所有資源注解標簽。
Application注解
從AndroidAnnotations 2.4開始,我們可以對application類使用注解標注了,用到的注解標簽為:@EApplication。除了和view、extras相關的注解標簽,其他的我們都可以用到這個類中。
@EApplication public class MyApplication extends Application {public void onCreate() {super.onCreate();initSomeStuff();}@SystemServiceNotificationManager notificationManager;@BeanMyEnhancedDatastore datastore;@RestServiceMyService myService;@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)UserDao userDao;@Backgroundvoid initSomeStuff() {// init some stuff in background} }從AndroidAnnotations 2.1開始,我們可以使用@APP標簽標注我們的application字段,不僅可以在Application類中,其他被注解標注的類中同樣可以使用。
@EActivity public class MyActivity extends Activity {@AppMyApplication application;} ============================================= @EBean public class MyBean {@AppMyApplication application;}從AndroidAnnotations 3.0開始,Application類必須由@EApplication進行標注
- 相關方法
從AndroidAnnotations 4.0開始:
Service注解
從AndroidAnnotations 2.4開始,我們可以使用@EService來標注service。
@EService public class MyService extends Service {}除了與views、extras相關的注解標簽,我們可以任意使用AA注解標簽。如:
@EService public class MyService extends IntentService {@SystemServiceNotificationManager notificationManager;@BeanMyEnhancedDatastore datastore;@RestServiceMyRestClient myRestClient;@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)UserDao userDao;public MyService() {super(MyService.class.getSimpleName());}@Overrideprotected void onHandleIntent(Intent intent) {// Do some stuff...showToast();}@UiThreadvoid showToast() {Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();} }使用注解開啟服務
startService(this, MyService_.class);
和activity一樣,在啟動服務時也要在服務名稱末尾添加“_”,如:通過內部構造器來增強服務:
MyService_.intent(getApplication()).start();從 AndroidAnnotations 3.0開始,通過內部構造器的stop方法來停止之前開啟的增強服務。
MyService_.intent(getApplication()).stop();intent綁定啟動服務
從AndroidAnnotation2.7開始,提供了一個啟動注解生成service的靜態幫助類。通過它我們可以:啟動service
// Starting the service MyService_.intent(context).start();綁定到service的intent
// Building an intent from the activity Intent intent = MyService_.intent(context).build();設置flags
// You can provide flags MyService_.intent(context).flags(Intent.FLAG_GRANT_READ_URI_PERMISSION).start();
intentService
從AndroidAnnotations 3.0開始,對于@EIntentService標注的類中的方法,我們可以通過@ServiceAction注解標簽來進行簡單的操作。至于@EService,除了與view、extras相關的注解標簽,我們幾乎可以任意使用。
我們可以通過內部構造器來增強intentService:
MyIntentService_.intent(getApplication()) //.myAction("test") //.start();如果在構造器中調用多個方法,將只執行最后一個。
從Since AndroidAnnotations 3.3開始注意:
即使IntentService#onHandleIntent 是抽象的,我們仍然需要添加一個空實現。為了簡便,AndroidAnnotations提供了AbstractIntentService 類,實現了該方法,所以我們不用再添加空實現了。SystemService
@EActivity public class MyActivity extends Activity {@SystemServiceNotificationManager notificationManager; }
@SystemService注解標簽表示Activity的字段被注入到對應的安卓系統服務中。相當于調用了 Context.getSystemService() 方法。相關方法
@EActivity public class MyActivity extends Activity {@SystemServicevoid setNotificationManager(NotificationManager notificationManager){// do something with notificationManager}void setMultipleServices(@SystemService NotificationManager notificationManager, @SystemService AudioManager audioManager){// do something with notificationManager and audioManager} }
從AndroidAnnotations 4.0開始:
broadcastreceivers注解
@EReceiver
@EReceiver public class MyReceiver extends BroadcastReceiver {}
從 AndroidAnnotations 2.4開始,我們可以使用@EReceiver注解標簽標注BroadcastReceiver。除了與view、extras相關的注解標簽,我們幾乎可以使用任意的AA注解。
@EReceiver public class MyReceiver extends BroadcastReceiver {@SystemServiceNotificationManager notificationManager;@BeanSomeObject someObject;}- @ReceiverAction
從AndroidAnnotations 3.2開始,我們可以使用此注解標簽在接受者中進行簡單的廣播操作。默認情況下,AndroidAnnotations 會根據被標注的方法名來判斷執行的操作,不過我們可以通過給標簽添加參數來傳遞另外一個操作。
被此標簽標注的方法可能包含以下參數:
- android.content.Context:void onReceive(Context context, Intent intent)傳遞過來額context;
- android.content.Intent:void onReceive(Context context, Intent intent)傳遞過來的intent;
- 任何可能通過intent傳遞的被@ReceiverAction.Extra標注的序列化參數(Parcelable 或Serializable 類型的參數)。
從Since AndroidAnnotations 3.3開始,注意:
即使BroadcastReceiver#onReceive是抽象的,我們仍然需要添加一個空實現。為了方便,AndroidAnnotations提供了 AbstractBroadcastReceiver類,實現了該方法,因此我們不再需要添加空實現了。注意:
我們可以通過添加@ReceiverAction的參數來添加多個動作。
我們可以通過dataScheme屬性 ,我們可以設置一個或多個接受者處理的數據類型。
@EReceiver public class MyIntentService extends BroadcastReceiver {@ReceiverAction(actions = android.content.Intent.VIEW, dataSchemes = "http")protected void onHttp() {// Will be called when an App wants to open a http website but not for https.}@ReceiverAction(actions = android.content.Intent.VIEW, dataSchemes = {"http", "https"})protected void onHttps() {// Will be called when an App wants to open a http or https website.}}- @Receiver
通過此標簽,我們可以向Activity/Fragment/service/intentService發送通知,而省掉了聲明 BroadcastReceiver的過程。
被@Receiver標記的方法 的參數:
- AndroidAnnotations 3.2之前,最多只有一個android.content.Intent。
- AndroidAnnotations 3.2之后,可能包含以下參數:
- android.content.Context:void onReceive(Context context, Intent intent)傳遞過來額context;
- android.content.Intent:void onReceive(Context context, Intent intent)傳遞過來的intent;
- 任何可能通過intent傳遞的被@Receiver.Extra標注的序列化參數(Parcelable 或Serializable 類型的參數)。
Registration(注冊)
用于在父類(Activity、Fragment、service)的生命周期中注冊或注銷一個廣播接收者。
registerAt屬性用來指定何時注冊或注銷廣播接收者。默認值為OnCreateOnDestroy。
下面這張圖標顯示了何時注冊/注銷會發生,并顯示了其屬性值在什么時候會用到。
示例:
@EFragment public class MyFragment extends Fragment {@Receiver(actions = "org.androidannotations.ACTION_1")protected void onAction1RegisteredOnCreateOnDestroy() {}@Receiver(actions = "org.androidannotations.ACTION_2", registerAt = Receiver.RegisterAt.OnAttachOnDetach)protected void onAction2RegisteredOnAttachOnDetach(Intent intent) {}@Receiver(actions = "org.androidannotations.ACTION_3", registerAt = Receiver.RegisterAt.OnStartOnStop)protected void action3RegisteredOnStartOnStop() {}@Receiver(actions = "org.androidannotations.ACTION_4", registerAt = Receiver.RegisterAt.OnResumeOnPause)protected void action4RegisteredOnResumeOnPause(Intent intent) {}}Local 廣播
注冊廣播時,設置local參數可以通過LocalBroadcastManager實現本地注冊,而不是使用父類的context。
local的默認值為false。
ContentProvider注解
從AndroidAnnotations 2.4開始,我們可以使用@EProvider注解標簽標注ContentProvider。
示例:
除了與view、extra相關的注解,我們幾乎可以使用所有的AA注解。
示例:
和TextView相關的注解
- HTML
AndroidAnnotations 2.2開始,我們可以通過@FromHtml和@HtmlRes兩個注解標簽來向TextView中添加HTML。
下面給出HTML字符串的資源文件:
@HtmlRes
此注解和@StringRes類似,都是綁定字符串資源。不同的是,次注解會通過調用HTML.fromHtml()方法來格式話字符串。
示例:
@FromHtml
當TextView被@ViewById標注后,可以使用此注解標注該TextView。作用是:將HTML字符串添加到TextView中。
示例:
文本變化監聽
此功能適用于AndroidAnnotations 2.6及之后。@TextChange
當指定TextView或其子類的TextView上的文本發生變化時,使用此注解可以接收到文本變化的監聽事件。此事件由android.text.TextWatcher.onTextChanged(CharSequence s, int start, int before, int count)定義。
此注解的參數可以是一個或多個TextView的R.id.*,如果沒有給定參數,將使用被標注的方法名作為參數。這個方法可能包含多個參數:- android.widget.TextView:明確監聽事件的view;
- java.lang.CharSequence:獲得被修改的文本;
- int start:修改的起始位置;
- int before:修改前文本的長度;
- int count:修改的文本個數。
示例:
@BeforeTextChange
當指定TextView或其子類的TextView上的文本發生變化之前,使用此注解可以接收到文本變化的監聽事件。此事件由android.text.TextWatcher.beforeTextChanged(CharSequence s, int start, int count, int after)定義。此注解的參數可以是一個或多個TextView的R.id.*,如果沒有給定參數,將使用被標注的方法名作為參數。這個方法可能包含多個參數:- android.widget.TextView:明確監聽事件的view;
- java.lang.CharSequence:獲取修改之前的文本;
- int start:修改的起始位置;
- int count:修改的文本個數;
- int after:修改后的文本長度。
示例:
@AfterTextChange
當指定TextView或其子類的TextView上的文本發生變化之后,使用此注解可以接收到文本變化的監聽事件。此事件由android.text.TextWatcher.afterTextChanged(Editable s)定義。此注解的參數可以是一個或多個TextView的R.id.*,如果沒有給定參數,將使用被標注的方法名作為參數。這個方法可能包含多個參數:- android.widget.TextView:明確監聽事件的view;
- android.text.Editable:在修改的文本上做改變。
示例:
@EditorAction
從AndroidAnnotations 3.1開始。
當指定TextView或其子類的TextView被編輯時,使用此注解可以接收到文本變化的監聽事件。此事件由android.widget.TextView.OnEditorActionListener#onEditorAction(android.widget.TextView, int, android.view.KeyEvent)定義。此注解的參數可以是一個或多個TextView的R.id.*,如果沒有給定參數,將使用被標注的方法名作為參數。這個方法可能包含多個參數:- android.widget.TextView:明確監聽事件的view;
- int 類型的參數:獲取actionId;
- android.view.KeyEvent;
這個方法的返回值類型可以是void或boolean。
- boolean:被標注的方法的返回值將被返回給生成的監聽方法(指示別人處理);
- void:監聽方法總是返回true(自己處理)。
示例:
SharedPreference注解
AndroidAnnotations 從2.1開始,提供了SharedPreferencesHelpers,允許我們使用SharedPreference。
定義SharedPreference
1、創建一個接口,并用 @SharedPref進行標注。如:
使用@Pref注解標簽,在Activity中創建定義的SharedPreferences 實例:
@EActivity public class MyActivity extends Activity {@PrefMyPrefs_ myPrefs;//注意:類型是帶“_”的 }在Activity中創建SharedPreferences 使用的類型,必須是生成的類,即名稱帶“_”的子類,千萬不要使用原始類名
相關方法
從AndroidAnnotations 4.0開始:
之后可以調用如下方法:
// Simple edit myPrefs.name().put("John");// Batch edit myPrefs.edit().name().put("John").age().put(42).apply();// Preference clearing: myPrefs.clear();// Check if a value exists: boolean nameExists = myPrefs.name().exists();// Reading a value long lastUpdated = myPrefs.lastUpdated().get();// Reading a value and providing a fallback default value long now = System.currentTimeMillis(); long lastUpdated = myPrefs.lastUpdated().getOr(now);設置默認值
從AndroidAnnotations 3.0開始,可以使用注解標簽設置默認值:@DefaultRes
示例:
使用字符串作為SharedPreferences的key
從AndroidAnnotations 3.1開始,我們可以使用strings文件中的字符串作為SharedPreferences的key,只需要將該字符串的ID設為keyRes的屬性值。如:
Scope
我們可以給通過設置value為下面的值來給SharedPreferences命名:
- ACTIVITY:名為MyActivity_MyPrefs的SharedPreferences專用;
- ACTIVITY_DEFAULT:名為MyActivity 的SharedPreferences專用;
- APPLICATION_DEFAULT:名為MyPrefs的SharedPreferences專用,是默認的SharedPreference 或UNIQUE。
因此,如果定義的接口需要單獨的SharedPreferences,為了保證所有的Activity能夠分享同一個SharedPreferences,我們應該這樣做:
在PreferenceActivity或PreferenceFragment中使用上面定義的SharedPreferences接口
public static String PREF_NAME = "MyPrefs";// in onCreate// Using your MyPrefs values this.getPreferenceManager().setSharedPreferencesName(PREF_NAME);// Opening the layout addPreferencesFromResource(R.xml.prefs);@PreferenceScreen
從AndroidAnnotations3.3開始,我們可以使用@PreferenceScreen注解來給組件(如Activity)添加preference布局了。此注解可以用在PreferenceActivity或PreferenceFragment或其子類上,同時,該組件必須用@EActivity或@EFragment標注。
如:
注意:如果同時我們還是用了@ViewById或@AfterViews,可能會出現view為null的情況。(我們可以在這里找到原因:https://github.com/excilys/androidannotations/issues/1574#issuecomment-148840535)
從AndroidAnnotations3.3.1開始,@PreferenceScreen支持對android.support.v4.PreferenceFragment和com.github.machinarius.preferencefragment.PreferenceFragment的標注。注意:這兩個類不是Android官方的類。
@PreferenceByKey
此注解可以用來標注Preference及其子類中的字段、PreferenceActivity或PreferenceFragment的子類,并且保證這些類已經被@EActivity或@EFragment標注。
此標注的值必須是字符串資源的id,和Preference的key關聯。
被標注的Preference必須是在@AfterPreference標注的方法中第一次出現。
例如:
相關方法
從AndroidAnnotations4.0開始:
@PreferenceChange
此標簽代表指定的Preference被改變時,被標注的方法會被調用,并會接收到OnPreferenceChangeListener.onPreferenceChange定義的事件。
此標簽的值是和Preference及其子類關聯的字符串資源的id,可以給定有多個。如果沒有設置值,被標注的方法名將作為該字符串資源的名稱進行綁定。
被標注的方法可能含有多個參數:
- Preference參數:標志此方法的目標preference;
- newVlaue:獲得這個preference的 Object、Set、Strings或String參數
如:
@PreferenceChange(R.string.myPref) void checkedChangedOnMyButton(boolean newValue, Preference preference) {// Something Here }@PreferenceChange void myPrefPreferenceChanged(Preference preference) {// Something Here }@PreferenceChange({R.string.myPref1, R.string.myPref2}) void preferenceChangeOnMultiplePrefs(Preference preference, String newValue) {// Something Here }@PreferenceChange(R.string.myPref) void preferenceChangeOnMyPref() {// Something Here }從AndroidAnnotations 3.3.1開始,Float、Integer、Long等原始 Android類也被支持作為上述參數中的newVlaue存在。因為Android Preference使用的是strings而不是numbers,AndroidAnnotations會自動解析strings,并將獲得的數值傳給被@PreferenceChange標注的方法。
例如:
從AndroidAnnotations 4.0開始,Preference的任意子類都能夠傳遞到方法中(如ListPreference)。
@PreferenceClick
此注解表示當Preference被用戶點擊時,被它標注的相對應的方法將會被觸發,并接收到OnPreferenceClickListener#onPreferenceClick定義的事件。
此注解的值是相應的Preference或其子類的id值,可以有多個。如果沒有設置值,那么方法名將被作為Preference的名稱進行關聯。
被它標注的方法可能含有這樣一個參數:
- Preference參數:表示被點擊的Preference對象。
如:
從AndroidAnnotations 4.0開始,任何Preference的子類都可以被傳送給被此注解標注的方法(如ListPreference)。
@PreferenceHeaders
此注解可以用來標注被@EActivity標注的PreferenceActivity的子類,向其注入資源中定義的preference headers。它的值是一個R.xml.*。
如:
@AfterPreferences
在addPreferenceFromResource被調用后,此注解標注的方法會被調用。addPreferenceFromResource會在super.onCreate()的末尾被調用。所有preference相關的代碼都在被此注解標注的方法中完成。
如:
事件綁定
@Click表明被此注解標注的方法必須和相對應的具有點擊功能的view進行綁定。我們可以通過給它設置參數來指定和誰綁定在一塊,此參數就是該view的id,如果不指定參數,框架會把方法的名字作為view的名字進行綁定,所以如果不給定參數的話,我們的參數要和控件的名字保持一致。
提示:方法不能設為私有。
例如:
從androidAnnotations4.0開始,view的任何子類都可以傳遞給方法(如button)中。
鍵盤的點擊事件
從AndroidAnnotations 4.0開始,在實現了KeyEvent.Callback接口的類中,我們可以很簡單的操作下面這四種和鍵盤相關的事件。
- @KeyDown
- @KeyUp
- @KeyLongPress
- @KeyMultiple
我們可以通過注解的參數來設置key code或key codes。使用KeyEvent.KEYCODE_*常量,我們可以很容易地傳遞codes。例如,
如果沒有設置key code,會認為方法的名稱就是key code。
如果Enter鍵的操作被調用,可能出現的命名是enter、onEnter、enterPressed、onEnterPressed。
被這四個注解標注的方法可能的返回值類型為void、boolean或Boolean。如果返回的是void,將被認為返回的是true(如:這個方法處理了這個事件)。
@KeyDown, @KeyUp, @KeyLongPress
被標注的方法可能有一個或沒有參數。如果有參數,這個參數只能是可能被觸發的那個keyEvent。注意:這個方法絕對不能是私有的。同一個類中,不同的方法不能處理同一個事件。
@KeyMultiple
被標注的方法可以有0-2個參數,這些參數可以是:
- int或Integer類型的參數:表示重復點擊的次數。
- 要觸發的keyEvent。
方法不能是私有的。同一個類中,不同的方法不能處理同一個事件。
例如:
PageChangeEvents
從AndroidAnnotations 4.0開始,當頁面的滑動狀態發生變化時,被此注解標注的方法會接收到android.support.v4.view.ViewPager.OnPageChangeListener.onPageScrollStateChanged(int state) 定義的事件。注解的值是android.support.v4.view.ViewPager的子類的R.id.*,可以是多個。如果沒有設置值,方法名會被用作這個值。被標注的方法可能有這樣參數:
- android.support.v4.view.ViewPager類型的參數:指定那個viewPager觸發這個事件;
- int類型的參數:獲取滑動狀態。
這些參數都是可選的。
例如:
@PageScrolled
當當前頁面滑動時,無論是程序啟動的平滑滾動的一部分還是用戶啟動的觸摸滾動,被注解的方法都會接收到android.support.v4.view.ViewPager.OnPageChangeListener.onPageScrolled(int position, float positionOffset, int positionOffsetPixels)定義的事件。注解的值是android.support.v4.view.ViewPager子類的R.id.*,可以是多個。如果沒有設置,方法名會被認為是該值。被標注的方法可能有以下幾個參數:
- android.support.v4.view.ViewPager:觸發事件的viewPager;
- int類型的參數:當前顯示的第一個頁面的position。如果positionOffset不是0,那么第position+1個頁面將會被顯示。
- float參數:范圍為【0,1),表示頁面滑動的偏移量,或者說偏移的百分比。
- int參數:頁面偏移的像素數。
這些參數都是可選的。
例如:
@PageSelected
當某一頁面被選擇時,被此注解標注的方法會接收到android.support.v4.view.ViewPager.OnPageChangeListener.onPageSelected(int position)定義的事件。它的值為 android.support.v4.view.ViewPager子類的R.id.*。如果值沒有設置,方法名將被認為是該值。被標注的方法可以有以下參數:
- android.support.v4.view.ViewPager:觸發事件的viewPager
- int參數:被選擇的頁面的position。
這些參數是可選的。
例如:
NonConfigurationInstance:配置改變,重新載入
從AndroidAnnotations 2.5開始,當配置中發生改變時,Activity會被銷毀并重新創建。這個特性對于重載資源是很有利的,但是我們經常需要在新舊Activity中進行圖片加載、網絡訪問、子線程等操作,這樣就很麻煩了。
這也就是我們需要這個注解的地方。我們可以使用這個注解標注在Activity中定義的字段,這樣就可以在配置改變時保留它們的實例,如橫豎屏切換時,保留當前Activity中的數據狀態。
如:
警告:我們可以使用這個注解標注除了和Activity向關聯的字段之外的任何字段,如Drawable、Adapter、View或其他和context相關的對象,都不可以被標注。如果標注了,將會引起這些資源或view的泄露,從而導致內存泄露。
上述警告內容不適用于被@Bean標注的實體類字段,因為AndroidAnnotations 自動地重新綁定其上下文。
總結
以上是生活随笔為你收集整理的关于androidannotations(注解)的理解和使用(文档篇)的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: bat一键修复打印机一直卡在正在删除提示
- 下一篇: webshell检测方式深度剖析 ---