Androidannotations Home
The project address on GitHub is androidannotations GitHub.
Wiki:https://github.com/excilys/androidannotations/wiki/availableannotations
1, using Dependency injection (Dependency injection) unfamiliar can understand inversion of Control (IoC)
2. Simplified threading model (simplified threading models)
3. Event Binding
4. REST Client
5, No Magic [it means: androidannotations at compile time will produce a subclass (next you will understand), you look at this subclass, you can see how it works]
The two most important jar packages in the project are: Androidannotations-api-3.0.1.jar and Androidannotations-3.0.1.jar
2). Create a new Android project, Then copy the Androidannotations-api-3.0.1.jar to the Libs directory, create a new folder at the root of the project, name Compile-libs, and then copy Androidannotations-3.0.1.jar to that directory .
3). Then set the project properties: Right-->properties->java compiler->annotation processing Select Enable project specific settings on this page.
4). Then click Annotation Processing's subkey factory path page, select Enable project specific settings, and then add the jar packages required for compilation. Click "Add JARs" to import the Androidannotations-3.0.1.jar from the previous Complie-libs directory and exit after saving.
Layout file XML using the way:
1<?xml version= "1.0" encoding= "Utf-8"?>2<linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"3android:orientation= "Vertical"4Android:layout_width= "Fill_parent"5android:layout_height= "Fill_parent"6>7<EditText8Android:id= "@+id/myinput"9Android:layout_width= "Fill_parent"Tenandroid:layout_height= "Wrap_content" One/> A<Button -Android:id= "@+id/mybutton" -Android:layout_width= "Fill_parent" theandroid:layout_height= "Wrap_content" -android:text= "click me!" -/> -<TextView +Android:id= "@+id/mytextview" -Android:layout_width= "Fill_parent" +android:layout_height= "Wrap_content" A/> at</LinearLayout>
Java file corresponding to activity:
1 PackageCom.example.testaa;2 3 Importorg.androidannotations.annotations.AfterViews;4 ImportOrg.androidannotations.annotations.Click;5 Importorg.androidannotations.annotations.EActivity;6 ImportOrg.androidannotations.annotations.UiThread;7 Importorg.androidannotations.annotations.ViewById;8 9 Importandroid.app.Activity;Ten Importandroid.content.Intent; One ImportAndroid.widget.Button; A ImportAndroid.widget.EditText; - ImportAndroid.widget.TextView; - ImportAndroid.widget.Toast; the @EActivity (R.layout.activity_main) - Public classMainactivityextendsActivity { - @ViewById (r.id.myinput) - EditText Myedittext; + - @ViewById + Button MyButton; A at @ViewById - TextView Mytextview; - /** - * Processing After the view has finished loading - */ - @AfterViews in voidafterviewprocess () { -Mybutton.settext ("Next"); toMytextview.settext ("First activity page"); + } - the /** * * Bind a point -and-click event $ */Panax Notoginseng @Click (R.id.mybutton) - voidProcessclick () { theIntent intent=NewIntent ( This, Subactivity_.class); +Intent.putextra ("Input_value", Myedittext.geteditabletext (). toString ()); A startactivity (intent); the } + - /** $ * Display a toast with the UI thread $ * @paramcontent - */ - @UiThread the voidShowtoast (String content) { - Toast.maketext (Getapplicationcontext (), content, Toast.length_short). Show ();Wuyi } the /** - * Time delay display Wu * @paramcontent - */ About@UiThread (delay=1000) $ voidShowtoastdelay (String content) { - Toast.maketext (Getapplicationcontext (), content, Toast.length_short). Show (); - } -}
As can be seen, we have greatly simplified the original code by means of annotations.
Note 1: @ViewById is similar to the Findviewbyid function, if the resource ID is not set after Viewbyid, it is automatically found with the same ID resource as the variable name (if the control variable name must be the same as the ID defined in the XML).
However, there is a problem, if called in the Click () method, the runtime will quote: NullPointerException error, we can not in the click () method use it directly, but in the method of @afterview annotation
NOTE 2: @Click Click event-handling annotations.
for @click, the method name can be written in the same way as the ID in the XML file, and Androidannotations is automatically recognized for multiple buttons, you can write multiple @click, or in this way:
@Click ({r.id.button1,r.id.button2,r.id.button3}) void buttonclicked (Button bt) { //TODO ...}
Note 3: @UiThread the annotations of the background UI thread, eliminating the handler and so on.
Note 4: @EActivity the annotations that prompt activity, notice that the annotation compiles the activity into activity_, noting that there is an underscore "_", so you need to add a slip line to the Androidmanifest.xml file
Reason: Using androidannotations, compile the time will generate a subclass, the name of the subclass is after the original class with an underscore "_", for example, this example produces a subclass named "Myactivity_", This requires you to register this activity, in the Androidmanifest.xml will be myactivity to Myactivity_, when used is also used myactivity_ to represent this class, If you're jumping from another activity to this program, use this:
Note 5: @AfterViews refers to code that executes after the view class has been injected.
The layout file for our second page is the same as the first one, and we'll look at its Java files:
PackageCom.example.testaa;Importorg.androidannotations.annotations.AfterViews;ImportOrg.androidannotations.annotations.Click;Importorg.androidannotations.annotations.EActivity;ImportOrg.androidannotations.annotations.Extra;ImportOrg.androidannotations.annotations.UiThread;Importorg.androidannotations.annotations.ViewById;Importandroid.app.Activity;ImportAndroid.widget.Button;ImportAndroid.widget.EditText;ImportAndroid.widget.TextView;ImportAndroid.widget.Toast, @EActivity (r.layout.activity_sub) Public classSubactivityextendsactivity{@ViewById (r.id.myinput) EditText myedittext; @ViewById Button MyButton; @ViewById TextView Mytextview; @Extra (Value= "Input_value") String inputstring; @AfterViewsvoidafterviewprocess () {mytextview.settext (inputstring); } @Click (R.id.mybutton)voidProcessclick () {Showtoast ("Clicked me!"); } @UiThreadvoidShowtoast (String content) {Toast.maketext (Getapplicationcontext (), content, Toast.length_short). Show (); }}
Note: A @extra annotation, the meaning of this annotation is the same as the Getintent (). Getextra (), in order to get the value passed over intent by the previous activity.
Androidmanifest.xml file:
1<?xml version= "1.0" encoding= "Utf-8"?>2<manifest xmlns:android= "Http://schemas.android.com/apk/res/android"3 Package= "Com.example.testaa"4Android:versioncode= "1"5Android:versionname= "1.0" >6 7<uses-SDK8Android:minsdkversion= "8"9Android:targetsdkversion= "/>"Ten One<Application AAndroid:allowbackup= "true" -android:icon= "@drawable/ic_launcher" -Android:label= "@string/app_name" theAndroid:theme= "@style/apptheme" > -<Activity -Android:name= "Com.example.testaa.MainActivity_" -Android:label= "@string/app_name" > +<intent-filter> -<action android:name= "Android.intent.action.MAIN"/> + A<category android:name= "Android.intent.category.LAUNCHER"/> at</intent-filter> -</activity> -<Activity -Android:name= "Com.example.testaa.SubActivity_" ></activity> -</application> - in</manifest>
Note: The activity of the Declaration, add more underline "_"
Android Open source framework: androidannotations