Android development framework androidannotations

Source: Internet
Author: User

I. Introduction
We often feel Spring is very convenient when developing J2EE projects. Therefore, when developing android, we naturally want to see if there is a similar framework. This blog mainly talks about the popular android development framework AndroidAnnotations. In addition to the dependency injection feature, it also integrates the Ormlite and Spring-android REST templates. It is very convenient to use and greatly improves the development efficiency.
Ii. Procedure
1. Download AndroidAnnotations:
$ Git clone https://github.com/excilys/androidannotations.git
Two important jar packages in the project are: androidannotations-api-3.0.1.jar and androidannotations-3.0.1.jar
2. Create an android project, copy the androidannotations-api-3.0.1.jar to the libs directory, create a new folder in the project's root directory, name it compile-libs, and then copy the androidannotations-3.0.1.jar to the directory

3. Set the project Properties: Right-click the project and choose Properties> Java Compiler> Annotation Processing. on this page, select Enable project specific settings.


Click the Factory Path page of the sub-item of Annotation Processing, select Enable project specific settings, and add the jar package required for compilation. Click Add JARs to import the androidannotations-3.0.1.jar under the previous complie-libs directory, save and exit.


4. Let's take a look at the following code:

First, layout files <喎?http: www.bkjia.com kf ware vc " target="_blank" class="keylink"> VcD48cD48cHJlIGNsYXNzPQ = "brush: java;">
The preceding layout file is no different from the layout file of a common project. Let's take a look at the java file of the corresponding Activity.

Package com. example. testaa; import org. androidannotations. annotations. afterViews; import org. androidannotations. annotations. click; import org. androidannotations. annotations. EActivity; import org. androidannotations. annotations. uiThread; import org. androidannotations. annotations. viewById; import android. app. activity; import android. content. intent; import android. widget. button; import android. widget. editText; import android. widget. textView; import android. widget. toast; @ EActivity (R. layout. activity_main) public class MainActivity extends Activity {@ ViewById (R. id. myInput) EditText myEditText; @ ViewByIdButton myButton; @ ViewByIdTextView myTextView;/*** process after loading the View */@ AfterViewsvoid afterViewProcess () {myButton. setText ("Next"); myTextView. setText ("first Activity page");}/*** bind a Click event */@ Click (R. id. myButton) void processClick () {Intent intent = new Intent (this, SubActivity _. class); intent. putExtra ("input_value", myEditText. getEditableText (). toString (); startActivity (intent);}/*** use the UI thread to display a Toast * @ param content */@ UiThreadvoid showToast (String content) {Toast. makeText (getApplicationContext (), content, Toast. LENGTH_SHORT ). show ();}/*** latency display * @ param content */@ UiThread (delay = 1000) void showToastDelay (String content) {Toast. makeText (getApplicationContext (), content, Toast. LENGTH_SHORT ). show ();}}

We can see that the original code is greatly simplified through annotation.

Note 1: @ ViewById and findViewById have similar functions. If no resource ID is set after ViewById, the id resource with the same name as the variable is automatically searched.

NOTE 2: @ Click event processing annotation.

NOTE 3: @ UiThread: the annotation of the background Ui thread saves Handler and so on.

Note 4: @ EActivity indicates the annotation of the Activity. Note that this annotation compiles the Activity into Activity _. Note that there is an underscore "_", so it is displayed in AndroidManifest. add a slide line to the xml file

Note 5: @ AfterViews refers to the Code executed after the View class is injected.


The layout file of the second page is the same as that of the first page. Let's take a look at its java file.

/* * $filename: SubActivity.java,v $ * $Date: 2014-4-15  $ * Copyright (C) ZhengHaibo, Inc. All rights reserved. * This software is Made by Zhenghaibo. */package com.example.testaa;import org.androidannotations.annotations.AfterViews;import org.androidannotations.annotations.Click;import org.androidannotations.annotations.EActivity;import org.androidannotations.annotations.Extra;import org.androidannotations.annotations.UiThread;import org.androidannotations.annotations.ViewById;import android.app.Activity;import android.widget.Button;import android.widget.EditText;import android.widget.TextView;import android.widget.Toast;/* *@author: ZhengHaibo   *web:     http://blog.csdn.net/nuptboyzhb *mail:    zhb931706659@126.com *2014-4-15  Nanjing,njupt,China */@EActivity(R.layout.activity_sub)public class SubActivity extends Activity{@ViewById(R.id.myInput)EditText myEditText;@ViewByIdButton myButton;@ViewByIdTextView myTextView;@Extra(value="input_value")String inputString;@AfterViewsvoid afterViewProcess(){myTextView.setText(inputString);}@Click(R.id.myButton)void processClick(){showToast("Clicked me !");}@UiThreadvoid showToast(String content){Toast.makeText(getApplicationContext(), content, Toast.LENGTH_SHORT).show();}}

Here, an @ Extra annotation is added. The meaning of this annotation is the same as that of getIntent (). getExtra (). The purpose is to obtain the value passed by the previous Activity through Intent.

The AndroidManifest. xml file is as follows:

 
     
                          
                                  
               
                      
 

Pay attention to the Activity declaration. The underscore "_" is added.

Entire project: http://download.csdn.net/detail/nuptboyzhb/7199005

Other annotations of the framework: https://github.com/excilys/androidannotations/wiki/AvailableAnnotations


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.