An analysis of Android annotations

Source: Internet
Author: User


Have encountered a lot of things, quite a long time not to update the blog, these two days learning this open-source framework Android Annotations, used to feel very convenient,

It is believed that children who have used spring annotations should understand easy!.

is to be more difficult to configure.

On the introduction of Androidannotaions, online has been very much, I am no longer a liability here.

1, androidannotations official website: http://androidannotations.org/(perhaps you need to FQ)

2. Description of the configuration method using Androidannotations in Eclipse: https://github.com/excilys/androidannotations/wiki/Eclipse-Project-Configuration

3. Configure androidannotations in Android Studio: (This is what I want to cover in this blog post!).


One, Android Studio configuration androidannotations environment.

1, first you set up a module, in the corresponding app will have a file named Build.gradle (the module is valid), and the entire project will have a file named Build.gradle (globally valid) " The application folder in this tool (equivalent to workspace under Eclipse) is capable of having multiple module (equivalent to project under Eclipse) "

2, we configure the time to be divided into the following two steps

In the local build.gradle (add the Red Font section ):

Apply plugin: ' Com.android.application '
Apply plugin: ' Android-apt '
def aaversion = ' 3.0.1 '



Android {
Compilesdkversion 19
Buildtoolsversion "20.0.0"


Defaultconfig {
ApplicationID "Com.tongbu.mytest"
Minsdkversion 8
Targetsdkversion 19
Versioncode 1
Versionname "1.0"
}
Buildtypes {
Release {
Runproguard false
Proguardfiles getdefaultproguardfile (' proguard-android.txt '), ' Proguard-rules.pro '
}
}
}


dependencies {
Compile Filetree (dir: ' Libs ', include: [' *.jar '])
Compile ' com.android.support:appcompat-v7:19.+ '

Apt "org.androidannotations:androidannotations: $AAVersion"
Compile "Org.androidannotations:androidannotations-api: $AAVersion"

}


Apt {
arguments {
Androidmanifestfile Variant.processResources.manifestFile
Resourcepackagename ' Com.tongbu.mytest '
}
}


In the global Build.gradle (Add Red font section ):

Buildscript {
repositories {
Jcenter ()
}
dependencies {
Classpath ' com.android.tools.build:gradle:0.12.+ '
classpath ' com.neenbedankt.gradle.plugins:android-apt:1.3 '
}
}


allprojects {
repositories {
Jcenter ()
}
}


Let's say the above has all gone smoothly. So congratulations you are almost ready to configure, you also need to add the name of the activity in the Androidmanifest.xml _ (underline), because this framework parsing and compiling, for example, the class mainactivity will be parsed into mainactivity_. Class So in the manifest file we add an underscore to the activity's name. Or androidannotation will make an error!

But it won't go so well. After you have finished adding the underline. You'll notice that you're not going to find mainactivity_ this stuff.

So what do we do?? We said that it was compiled at the time of the whole. Then we just have to click on the Compile button to build!!



This way the androidannotation environment on Android Studio is configured. Eclipse is a lot more informative. It's not going to be introduced here.


Second demo to learn some of Androidannotations's annotations

@NoTitle//Untitled @fullscreen//fullscreen @eactivity (r.layout.activity_my) public class MyActivity extends Actionbaractivity {//==    ============================================ main annotations ================================================= @ViewById    Button button1;    @ViewById Button button2;    @ViewById (R.ID.TEXTVIEW1)//The injected TextView TextView of the specified ID;    @ViewById ProgressBar ProgressBar;    @ViewById ImageView ImageView; Gets the method of the system SERVICE (instead of the original Clipboardmanager = (Clipboardmanager) getsystemservice (clipboard_service);) @SystemService Cl    Ipboardmanager Clipboardmanager; @Click ({r.id.button1,r.id.button2,r.id.button3,r.id.button4}) public void simplebuttononclicked (view view) {Swit            CH (view.getid ()) {case r.id.button1:{Textview.settext ("Button1 is clicked!");            } break;            Case r.id.button2:{Textview.settext ("Button2 is clicked!");            } break;       Case r.id.button3:{         String content = Clipboardmanager.gettext (). toString ();            Toast.maketext (Getapplicationcontext (), "Clipboard contents:" + Content, Toast.length_short). Show ();            } break;                Case r.id.button4:{Toast.maketext (Getapplicationcontext (), "scroll bar starts!", Toast.length_short). Show ();            Progressbarworks ();        } break;            }} @LongClick ({r.id.button2}) public void buttononlongclicked (view view) {switch (View.getid ()) { Case r.id.button1:{Textview.settext ("Button1 is longclicked!");            /Because there is no registration, it cannot be triggered} break; Case r.id.button2:{Textview.settext ("Button2 is longclicked!");        /can trigger} break; }}//=================================================== notes on resources ========================================= @Animat    Ionres (r.anim.rotate) Animation animationrotate; @DrawableRes (R.drawable.myphoto) drawabLe Myphoto;    @ColorRes (r.color.love) Integer MyColor;    @TextRes (r.string.textres) charsequence text; @Click ({r.id.button5,r.id.button6,r.id.button7}) public void animationbuttononclicked (view view) {switch (VIEW.G            Etid ()) {case r.id.button5:{imageview.startanimation (animationrotate);            } break;            Case r.id.button6:{imageview.setimagedrawable (Myphoto);            } break;            Case r.id.button7:{Toast.maketext (Getapplicationcontext (), text.tostring (), Toast.length_short). Show ();        } break; }}//============================================== annotations on threads ================================================//equivalent to  A new task Asynctask or a new thread @Background public void Progressbarworks () {//is equivalent to running in a new thread: @Background int        i = 1;            while (i <=) {LOG.E ("Progress", "Progress:" + i); try {ThRead.sleep (1000);                UpdateProgressBar (i);            Direct progressbar.setprogress (i); can also, so the @background annotation inside may realize the handler mechanism i++;            } catch (Interruptedexception e) {e.printstacktrace ();        }}}//refers to the UI thread @UiThread public void UpdateProgressBar (int i) {progressbar.setprogress (i);        if (i = =) {Toast.maketext (Getapplicationcontext (), "Scroll bar End", Toast.length_short). Show ();     }}//======================================= on the sequencing of several events =============================================== @Override        protected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate);        LOG.E ("Firsttolast", "onCreate"); Can be omitted!

Setcontentview (r.layout.activity_my); Progressbar.setmax (100); Error, NULL pointer exception//due to OnCreate () being called. @ViewById is not set, that is, NULL//So suppose you want to initialize the component, then you need to use @afterviews annotation} @AfterViews public void init () { LOG.E ("Firsttolast", "Init"); Progressbar.setmax (10); } @Override protected void Onresume () {super.onresume (); LOG.E ("Firsttolast", "Onresume"); } @Override protected void OnStart () {Super.onstart (); LOG.E ("Firsttolast", "OnStart"); }}



Sequence of successive calls of several methods:



Notes on resources (not listed below):


Android Annotations Analysis

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.