Android-annotations getting started

Source: Internet
Author: User
Tags xml example

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
  1. Package = "com. example. hello"
  2. Android: versionCode = "1"
  3. Android: versionName = "1.0" type = "codeph" text = "/codeph">
  4. Android: minSdkVersion = "14"
  5. Android: targetSdkVersion = "18"/>
  6. Android: allowBackup = "true"
  7. Android: icon = "@ drawable/ic_launcher"
  8. Android: label = "@ string/app_name">
  9. Android: name = "com. example. hello. MainActivity _"
  10. Android: label = "@ string/app_name">
  11. 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
    1. @ EActivity (R. layout. activity_main)
    2. Public class MainActivity extends Activity {
    3. @ ViewById (R. id. myInput)
    4. EditText myInput;
    5. @ ViewById (R. id. myTextView)
    6. TextView textView;
    7. @ ViewById (R. id. myButton2)
    8. Button btn2;
    9. @ StringRes (R. string. go_to_second)
    10. String btn2Txt;
    11. @ AfterViews
    12. Protected void afterViews (){
    13. Btn2.setText (btn2Txt );
    14. }
    15. @ Click
    16. Void myButton (){
    17. String name = myInput. getText (). toString ();
    18. TextView. setText ("Hello" + name );
    19. }
    20. @ Click
    21. Void myButton2 (){
    22. SecondActivity _. intent (this). start ();
    23. }
    24. }
    25. Activity_main.xml
    26. Xmlns: tools = "http://schemas.android.com/tools"
    27. Android: layout_width = "match_parent"
    28. Android: layout_height = "match_parent"
    29. Android: orientation = "vertical">
    30. Android: id = "@ + id/myInput"
    31. Android: layout_width = "fill_parent"
    32. Android: layout_height = "wrap_content"/>
    33. Android: id = "@ + id/myButton"
    34. Android: layout_width = "fill_parent"
    35. Android: layout_height = "wrap_content"
    36. Android: text = "Click me! "/>
    37. Android: id = "@ + id/myTextView"
    38. Android: layout_width = "fill_parent"
    39. Android: layout_height = "wrap_content"/>
    40. Android: id = "@ + id/myButton2"
    41. Android: layout_width = "fill_parent"
    42. Android: layout_height = "wrap_content"
    43. Android: text = "@ string/go_to_second"/>
    44. 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
      1. Maven-compiler-plugin
      2. 3.1
      3. UTF-8
      4. 1.6
      5. 1.6
      6. -Atrace = true
      7. -AlogLevel = trace
      8. -AlogConsoleAppender = true
      9. -AandroidManifestFile =/path/to/AndroidManifest. xml
      10. 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
        1. Public final class MainActivity _ extends MainActivity {
        2. @ Override
        3. Public void onCreate (Bundle savedInstanceState ){
        4. Init _ (savedInstanceState); // @ StringRes, @ ColorRes, and so on will be processed here.
        5. Super. onCreate (savedInstanceState );
        6. SetContentView (layout. activity_main); // @ EActivity (R. layout. activity_main)
        7. }
        8. Private void init _ (Bundle savedInstanceState ){
        9. Resources resources _ = this. getResources ();
        10. Btn2Txt = resources _. getString (string. go_to_second );
        11. }
        12. @ Override
        13. Public void setContentView (int layoutResID ){
        14. Super. setContentView (layoutResID );
        15. AfterSetContentView _();
        16. }
        17. @ Override
        18. Public void setContentView (View view, LayoutParams params ){
        19. Super. setContentView (view, params );
        20. AfterSetContentView _();
        21. }
        22. @ Override
        23. Public void setContentView (View view ){
        24. Super. setContentView (view );
        25. AfterSetContentView _();
        26. }
        27. Private void afterSetContentView _(){
        28. // @ ViewById
        29. TextView = (TextView) findViewById (id. myTextView ));
        30. MyInput = (EditText) findViewById (id. myInput ));
        31. Btn2 = (Button) findViewById (id. myButton2 ));
        32. // @ Click
        33. {
        34. View view = findViewById (id. myButton2 );
        35. If (view! = Null ){
        36. View. setOnClickListener (new OnClickListener (){
        37. @ Override
        38. Public void onClick (View view ){
        39. MainActivity _. this. myButton2 ();
        40. }
        41. }
        42. );
        43. }
        44. }
        45. // @ Click
        46. {
        47. View view = findViewById (id. myButton );
        48. If (view! = Null ){
        49. View. setOnClickListener (new OnClickListener (){
        50. @ Override
        51. Public void onClick (View view ){
        52. MainActivity _. this. myButton ();
        53. }
        54. }
        55. );
        56. }
        57. }
        58. AfterViews (); // The AfterViews () method marked by @ afterViews is called here.
        59. }
        60. Public static MainActivity _. IntentBuilder _ intent (Context context ){
        61. Return new MainActivity _. IntentBuilder _ (context );
        62. }
        63. Public static class IntentBuilder _ {// This is for startActivity and startActivityForResult
        64. Private Context context _;
        65. Private final Intent intent _;
        66. Public IntentBuilder _ (Context context ){
        67. Context _ = context;
        68. Intent _ = new Intent (context, MainActivity _. class );
        69. }
        70. Public Intent get (){
        71. Return intent _;
        72. }
        73. Public MainActivity _. IntentBuilder _ flags (int flags ){
        74. Intent _. setFlags (flags );
        75. Return this;
        76. }
        77. Public void start (){
        78. Context _. startActivity (intent _);
        79. }
        80. Public void startForResult (int requestCode ){
        81. If (context _ instanceof Activity ){
        82. (Activity) context _). startActivityForResult (intent _, requestCode );
        83. } Else {
        84. Context _. startActivity (intent _);
        85. }
        86. }
        87. }
        88. }

          Finally take a look at the complete pom. xml example (https://github.com/excilys/androidannotations/blob/develop/examples/maveneclipse/pom.xml ):

          [Html]View plaincopy
          1. 4.0.0
          2. Org. androidannotations
          3. Maveneclipse
          4. 0.0.1-SNAPSHOT
          5. Apk
          6. Maveneclipse
          7. UTF-8
          8. 4.1.1.4
          9. 16
          10. 3.2
          11. 1.6
          12. Com. google. android
          13. Android
          14. $ {Android. version}
          15. Provided
          16. Org. androidannotations
          17. Androidannotations
          18. $ {Androidannotations. version}
          19. Provided
          20. Org. androidannotations
          21. Androidannotations-api
          22. $ {Androidannotations. version}
          23. Maven-compiler-plugin
          24. 3.1
          25. $ {Java. version}
          26. $ {Java. version}
          27. -AlogLevel = trace
          28. Com. jayway. maven. plugins. android. generation2
          29. Android-maven-plugin
          30. 3.9.0-rc.3
          31. $ {Android. platform}
          32. True
          33. True
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.