Android-annotations getting started
Reprinted please indicate the source: http://write.blog.csdn.net/postedit/41577317
Androidannotation is a very awesome framework (https://github.com/excilys/androidannotations/wiki) that can do: Dependency Injection, Simplified thread model (Simplified threading model), Event binding ), REST Client.
Very easy to use, more importantly, it has no impact on performance! In this article, let's take a brief look at some entry points.
1. Starting from the example (refer to the https://github.com/excilys/androidannotations/wiki/FirstActivity ):
AndroidManifest. xml
[Html]View plaincopy
-
- Package = "com. example. hello"
- Android: versionCode = "1"
- Android: versionName = "1.0" type = "codeph" text = "/codeph">
- Android: minSdkVersion = "14"
- Android: targetSdkVersion = "18"/>
- Android: allowBackup = "true"
- Android: icon = "@ drawable/ic_launcher"
- Android: label = "@ string/app_name">
- Android: name = "com. example. hello. MainActivity _"
- Android: label = "@ string/app_name">
-
-
-
-
-
-
-
- It defines two activities. Note that the names are followed by an underscore.
2. MainActivity. java: note that the name is not underlined.
[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
- Xmlns: tools = "http://schemas.android.com/tools"
- Android: layout_width = "match_parent"
- Android: layout_height = "match_parent"
- Android: orientation = "vertical">
- Android: id = "@ + id/myInput"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"/>
- Android: id = "@ + id/myButton"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "Click me! "/>
- Android: id = "@ + id/myTextView"
- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"/>
-
Android: id = "@ + id/myButton2"- Android: layout_width = "fill_parent"
- Android: layout_height = "wrap_content"
- Android: text = "@ string/go_to_second"/>
- The source code of SecondActivity is not pasted.
After injection, the code looks very concise and there will be no more findViewById and so on.
How to compile and run it (see https://github.com/excilys/androidannotations/wiki/CustomizeAnnotationProcessing ):
(1) If javac is used, you must pass the-AandroidManifestFile =/path/to/AndroidManifest. xml parameter to specify the Manifest file.
(2) If Eclipse is used, right-click the project and choose Properties> Java Compiler> Annotation Processing> Enable annotation processing,
Then add the jar package of AndroidAnnotations in the Factory Path.
(3) If maven is used, you can set compilation parameters in version 3.1 of maven-compiler-plugin.
[Html]View plaincopy
-
- Maven-compiler-plugin
- 3.1
-
- UTF-8
- 1.6
- 1.6
-
- -Atrace = true
- -AlogLevel = trace
- -AlogConsoleAppender = true
- -AandroidManifestFile =/path/to/AndroidManifest. xml
-
-
- All available parameters are as follows:
(1) trace: boolean, used to enable or disable the @ Trace annotation. This annotation is executed using the log record method.
(2) androidManifestFile: string. By default, AndroidAnnotations recursively searches for AndroidManifest. xml in its parent folder. If your project has a special structure, you can use this to specify.
(3) resourcePackageName: string. By default, AndroidAnnotations are retrieved from AndroidManifest. extract the application package from the xml file to find the R file. If the R file is in a custom package, you can use it to set it.
(4) logFile: string. Starting from 3.0, AndroidAnnotations uses a custom logger to record the logs during code execution. Logs are written in the {outputFolder}/androidannotations. log File by default.
The {outputFolder} parameter is searched in the following order: target-> build-> bin-> root project folder. If {outputFolder} cannot be found, you can use logFile to specify the output file. {OutputFolder} can use placeholders to dynamically change the file name.
(5) logLevel: string, enum (trace, debug, info, warn, error): the default log level is DEBUG, which may be useful if it is changed to TRACE.
(6) logAppenderConsole: boolean. By default, AndroidAnnotations will use FileAppender and messagerappendertwo log appender. FileAppender will be written into the log file, and MessagerAppender will be displayed in the IDE message list. You can set logAppenderConsole to true to enable another leleappender.
(7) threadControl: boolean, used to enable or disable @ SupposeUiThread and @ SupposeBackground annotations. The default value is true, which ensures that the method is called in the correct thread.
3. Impact on Performance
No effect! Because it is used to generate a subclass of the Activity class, instead of using reflection! Let's take a look at the activity automatically generated by the compiler for us:
[Java]View plaincopy
- Public final class MainActivity _ extends MainActivity {
- @ Override
- Public void onCreate (Bundle savedInstanceState ){
- Init _ (savedInstanceState); // @ StringRes, @ ColorRes, and so on will be processed here.
- 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 (); // The AfterViews () method marked by @ afterViews is called here.
- }
- Public static MainActivity _. IntentBuilder _ intent (Context context ){
- Return new MainActivity _. IntentBuilder _ (context );
- }
- Public static class IntentBuilder _ {// This is for startActivity and 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 _);
- }
- }
- }
- }
Finally take a look at the complete pom. xml example (https://github.com/excilys/androidannotations/blob/develop/examples/maveneclipse/pom.xml ):
[Html]View plaincopy
-
-
- 4.0.0
- Org. androidannotations
- Maveneclipse
- 0.0.1-SNAPSHOT
- Apk
- Maveneclipse
-
-
- UTF-8
- 4.1.1.4
- 16
- 3.2
- 1.6
-
-
-
-
- Com. google. android
- Android
- $ {Android. version}
- Provided
-
-
- Org. androidannotations
- Androidannotations
- $ {Androidannotations. version}
- Provided
-
-
- Org. androidannotations
- Androidannotations-api
- $ {Androidannotations. version}
-
-
-
-
-
-
- Maven-compiler-plugin
- 3.1
-
- $ {Java. version}
- $ {Java. version}
-
- -AlogLevel = trace
-
-
-
-
- Com. jayway. maven. plugins. android. generation2
- Android-maven-plugin
- 3.9.0-rc.3
-
-
- $ {Android. platform}
-
- True
-
- True
-
-
-
-