1. Configuration
First go to git to download the jar package, one is Androidannotations.jar, one is Androidannotations-api.jar.
Create a new Android project, put Androidannotations-api.jar into the Libs directory, and then create a new directory Compile-lib, androidannotations to be enterprising, Right click on our engineering-->properties-->java compiler-->annotation processing--> tick enable project specific Settings and the bottom two selection boxes, then click on the left factory Path, add our just compile-lib in the jar package, can.
2. Use of the framework
Note that after using Androidannotation, all four components need to be appended with a "_" after their names.
(1). @EActivity
Use the @ symbol above the activity to add annotations, such as:
@EActivity (r.layout.activity_main) public class Mainactivity extends activity {<span style= "White-space:pre" > </span> @ViewById (r.id.button1) <span style= "White-space:pre" ></span>button button;<span style = "White-space:pre" ></span> @Override <span style= "White-space:pre" ></span>protected void OnCreate (Bundle savedinstancestate) {<span style= "White-space:pre" ></span>super.oncreate ( savedinstancestate) <span style= "White-space:pre" ></span>}<span style= "White-space:pre" ></ Span> @Click (r.id.button1) <span style= "White-space:pre" ></span>public void Startacitvity () {<span Style= "White-space:pre" ></span>startactivity (New Intent (Mainactivity.this, secondactivity_.class));< Span style= "White-space:pre" ></SPAN>}}
It is easy to see how we can save our previous Findviewbyid.
(2). @EService
Create a new service:
@EServicepublic class MyService extends Service {@Overridepublic int onstartcommand (Intent Intent, int flags, int startid) {log.i ("THR", "Onstartcommand"); return Super.onstartcommand (Intent, flags, Startid);} @Overridepublic ibinder onbind (Intent Intent) {return null;}}
The same as the activity:
@Click (r.id.button1) public void StartService () {StartService (new Intent (this, myservice_.class));}
Note that all needs to be configured in the manifest file, and the underscore "_" cannot be forgotten.
(3). @ViewById Find Control
@ViewById (r.id.textview1) TextView textView1; @ViewById (r.id.textview2) TextView textView2; @AfterViewspublic void Settextview () {Textview1.settext ("Text1"); Textview2.settext ("Text2");}
If you do not specify the ID after Viewbyid, it will default to look for the same ID as our variable name, if found can run normally, cannot find the exception that will report null pointer.
(4). @ViewsById Find Multiple controls
There is also a way to use Viewsbyid:
@ViewsById ({r.id.textview1, r.id.textview2}) list<textview> List; @AfterViewspublic void Settextview () { List.get (0). SetText ("Text1"); List.get (1). SetText ("Text2");}
also very simple.
(5). @Click Click events
<span style= "White-space:pre" ></span> @ViewById (R.id.textview1) < Span style= "White-space:pre" ></span>textview textview1;<span style= "White-space:pre" ></span> @ViewById <span style= "White-space:pre" ></span>textview textview2;<span style= "White-space:pre" > </span> @Click ({r.id.textview1, r.id.textview2}) <span style= "White-space:pre" ></span>public void Showtoast () {<span style= "White-space:pre" ></span>toast.maketext (This, "click event", 1). Show (); <span style = "White-space:pre" ></span>}<span style= "White-space:pre" ></span> @AfterViews <span style= " White-space:pre "></span>public void Settextview () {<span style=" White-space:pre "></span> Textview1.settext ("TextView1"), <span style= "White-space:pre" ></span>textview2.settext ("TextView2"); <span style= "White-space:pre" ></SPAN>}
With @click annotations, you can also add click events to multiple views.
The use of @LongClick is similar to this one, this is not introduced here.
(6). @Extra can easily pass parameters
In the first activity:
public static final String name = ' name ';p ublic static final string age = ' age '; @Click (r.id.button1) public void Startactiv ity () {Intent Intent = new Intent (this, secondactivity_.class); Intent.putextra (NAME, "Jerry"); Intent.putextra (age, 27) ; StartActivity (intent);}
in secondactivity:
@Extra (mainactivity.name) String NAME; @Extra (mainactivity.age) int age; @ViewById (R.id.name_view) TextView nameview;@ Viewbyid (R.id.age_view) TextView ageview; @AfterViewspublic void Initview () {nameview.settext (name); Ageview.settext ( Age + "");}
(7). @ItemClick, @ItemLongClick and @touch
This is mainly used on the ListView:
@ViewById (R.id.listview) ListView listview; @ItemClick (R.id.listview) public void Listviewitemclick (int p) { Toast.maketext (this, p + "good", 0). Show ();
Itemlongclick is similar to this method of use.
(8). @Background methods for handling background tasks
@Backgroundpublic void DoSomething () {log.i ("THR", "Background thread ID" + thread.currentthread (). GetId ());} @Click (r.id.button1) public void StartActivity () {dosomething ();} @Overrideprotected void OnCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); LOG.I ("THR", "Mainactivity thread id" + thread.currentthread (). GetId ());}
By running discovery, this method is run in a child thread.
(9). @UiThread
To modify the code:
@Backgroundpublic void DoSomething () {log.i ("THR", "Background thread ID" + thread.currentthread (). GetId ()); UpdateUi ();} @UiThreadpublic void UpdateUi () {<pre name= "code" class= "java" ><pre name= "code" class= "Java" >log.i ("THR", "UpdateUI thread ID" + thread.currentthread (). GetId ());
Textview1.settext ("Update UI");}
You can make the update UI action called in the main thread, without reporting an exception.
(Ten). @StringRes get string in the resource file
@StringRes (r.string.name) string name;
And the use of @dimensionres is similar:
@DimensionRes (r.dimen.activity_horizontal_margin) double size;
(one). @WindowFeature window Style
Untitled style:
@WindowFeature ({window.feature_no_title, window.feature_indeterminate_progress}) @EActivity (r.layout.activity_ Main) public class Mainactivity extends Activity {
4. View Activity_
Right-click Project-->java compiler-->annotation processing-->generated Source directory:--> Remove the points in front of apt_generated.
5. Summary
Here are a few things to keep in mind when using this framework:
(1). Register activity_ with the same name
(2). The decoration of the view cannot be private
(3). The method and field of the annotation annotation cannot be private
LOG.I ("THR", "Background thread ID" + thread.currentthread (). GetId ());
Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.
"Android Advanced" (1) using the Open source framework Androidannotation