Android's hottest high-speed development framework Androidannotations configuration specifically explains the eclipse configuration steps in the article. Introduction to the Androidannotations Introduction to Android's hottest high-speed development framework. This article focuses on the use of annotation methods in the commentary androidannotations.
@EActivity
Demo Sample:
@EActivity (r.layout.main) public class MyActivity extends Activity {}
@fragment
Demo Sample:
@EFragment (r.layout.my_fragment_layout) public class Myfragment extends fragment {}
Note:
Create:
Myfragment fragment = new Myfragment_ ();
General class:
@EBeanpublic class MyClass {}
Note: This class must have only one constructor. There is a maximum number of parameters in a context.
Use in activity:
@EActivitypublic class MyActivity extends Activity { @Bean myotherclass myotherclass;}
can also be used to declare interfaces:
@Bean (myimplementation.class) MyInterface MyInterface;
The root environment can also be injected into the normal class:
@EBeanpublic class MyClass { @RootContext context context; Only injected if the root context was an activity @RootContext activity activity; Injected if the root context is a service @RootContext Service service; Only injected if the root context was an instance of myactivity @RootContext myactivity myactivity;}
Suppose you want to do something at class creation time to:
@AfterInject public void Dosomethingafterinjection () { //Notificationmanager and dependency is set }
The Singleton class needs such as the following declaration:
@EBean (scope = Scope.singleton) public class Mysingleton {}
Note: It is not possible to inject view and event bindings into a singleton class, because the singleton life cycle is longer than the activity and service, so that memory overflow is not possible.
@EView
@EViewpublic class CustomButton extends Button { @App myapplication application; @StringRes String Somestringresource; Public CustomButton (context context, AttributeSet Attrs) { Super (context, attrs); }}
Note:
Create:
CustomButton button = custombutton_.build (context);
@EViewGroup
@EViewGroup (r.layout.title_with_subtitle) public class Titlewithsubtitle extends Relativelayout { @ViewById Protected TextView title, subtitle; Public Titlewithsubtitle (context context, AttributeSet Attrs) { Super (context, attrs); } public void Settexts (string titletext, String subtitletext) { title.settext (titletext); Subtitle.settext (Subtitletext); }}
Note:
@EApplication
@EApplicationpublic class MyApplication extends application {}
Use in activity:
@EActivitypublic class MyActivity extends Activity { @App myapplication Application;}
@EService
@EServicepublic class MyService extends Service {}
Jump Service:
Myservice_.intent (Getapplication ()). Start ();
Stop service:
Myservice_.intent (Getapplication ()). Stop ();
@EReceiver
@EReceiverpublic class Myreceiver extends Broadcastreceiver {}
@Receiver can replace the declaration broadcastreceiver
@EActivitypublic class MyActivity extends Activity { @Receiver (actions = "org.androidannotations.ACTION_1") protected void OnAction1 () { }}
@EProvider
@EProviderpublic class Mycontentprovider extends ContentProvider {}
@ViewById
@EActivitypublic class MyActivity extends Activity { //injects R.id.myedittext, the variable name must match the ID name of the layout @ViewById EditText Myedittext; @ViewById (R.id.mytextview) TextView TextView;}
@AfterViews
@EActivity (r.layout.main) public class MyActivity extends Activity { @ViewById TextView mytextview; @AfterViews void Updatetextwithdate () {Be sure to do some of the view settings here. Do not set in OnCreate () because OnCreate () is not injected at runtime view
Mytextview.settext ("Date:" + new Date ()); } [...]
@StringRes
@EActivitypublic class MyActivity extends Activity { @StringRes (R.string.hello) string myhellostring;// cannot be set to a private variable @StringRes String Hello;}
@ColorRes
@EActivitypublic class MyActivity extends Activity { @ColorRes (r.color.backgroundcolor) int somecolor; @ColorRes int backgroundcolor;}
@AnimationRes
@EActivitypublic class MyActivity extends Activity { @AnimationRes (R.anim.fadein) Xmlresourceparser Xmlresanim; @AnimationRes Animation Fadein;}
@DimensionRes
@EActivitypublic class MyActivity extends Activity { @DimensionRes (r.dimen.fontsize) float fontsizedimension ; @DimensionRes float fontsize;}
@DImensionPixelOffsetRes
@EActivitypublic class MyActivity extends Activity { @DimensionPixelOffsetRes (r.string.fontsize) int fontsizedimension; @DimensionPixelOffsetRes int fontsize;}
@DimensionPixelSizeRes
@EActivitypublic class MyActivity extends Activity { @DimensionPixelSizeRes (r.string.fontsize) int fontsizedimension; @DimensionPixelSizeRes int fontsize;}
Other Res:
@BooleanRes
@ColorStateListRes
@DrawableRes
@IntArrayRes
@IntegerRes
@LayoutRes
@MovieRes
@TextRes
@TextArrayRes
@StringArrayRes
@Extra
@EActivitypublic class MyActivity extends Activity { @Extra ("Mystringextra") String mymessage; @Extra ("Mydateextra") date Mydateextrawithdefaultvalue = new Date ();
Or:
@EActivitypublic class MyActivity extends Activity { //The name of the extra will be "Mymessage", the name must be consistent @Extra C18/>string Mymessage;}
Pass Value:
Myactivity_.intent (). Mymessage ("Hello"). Start ();
@SystemService
@EActivitypublic class MyActivity extends Activity {// @SystemService Notificationmanager Notificationmanager;}
@HtmlRes
@EActivitypublic class MyActivity extends Activity { //injects r.string.hello_html @HtmlRes (r.string.hello_ HTML) spanned myhellostring; Also injects r.string.hello_html @HtmlRes charsequence hellohtml;}
@FromHtml
@EActivitypublic class MyActivity extends Activity {//must be used in TextView @ViewById (r.id.my_text_view) @FromHtml ( r.string.hello_html) TextView TextView; Injects r.string.hello_html into the r.id.hello_html view @ViewById @FromHtml TextView hellohtml;}
@NonConfigurationInstance
public class MyActivity extends Activity {//equals activity.onretainnonconfigurationinstance () @ Nonconfigurationinstance Bitmap Somebitmap; @NonConfigurationInstance @Bean mybackgroundtask mybackgroundtask;}
@HttpsClient
@HttpsClientHttpClient httpsclient;
Demo Sample:
@EActivitypublic class MyActivity extends Activity { @HttpsClient (truststore=r.raw.cacerts, truststorepwd= " Changeit ", hostnameverif=true) HttpClient httpsclient; @AfterInject @Background public void Securedrequest () { try { HttpGet httpget = new HttpGet ("https:// www.verisign.com/"); HttpResponse response = Httpsclient.execute (httpget); Dosomethingwithresponse (response); } catch (Exception e) { e.printstacktrace (); } } @UiThread public void Dosomethingwithresponse (HttpResponse resp) { Toast.maketext (this, "HTTP status" + Resp.getstatusline (). Getstatuscode (), Toast.length_long). Show (); }}
@FragmentArg
@EFragmentpublic class Myfragment extends Fragment {//equals Fragment Argument @FragmentArg ("Mystringargument") String Mymessage; @FragmentArg String anotherstringargument; @FragmentArg ("Mydateextra") date Mydateargumentwithdefaultvalue = new Date ();
Myfragment myfragment = Myfragment_.builder () . Mymessage ("Hello"). anotherstringargument ("World") . Build ();
@Click
@Click (R.id.mybutton) void mybuttonwasclicked () { [...]} @Clickvoid Anotherbutton () {//assumes that the function name and ID do not specify the corresponding [...]} @Clickvoid Yetanotherbutton (View clickedview) { [...]}
Other Click events:
Clicks with @Click
Long clicks with @LongClick
Touches with @Touch
Adapterviewevents
Item clicks with @ItemClick
Long Item clicks with @ItemLongClick
The Item selection with @ItemSelect has two ways of calling: 1.
@EActivity (r.layout.my_list) public class Mylistactivity extends Activity { //... @ItemClick public void mylistitemclicked (myitem Clickeditem) {//myitem is an entity class of adapter, equivalent to Adapter.getitem (position ) } @ItemLongClick public void mylistitemlongclicked (myitem clickeditem) { } @ItemSelect public void Mylistitemselected (Boolean selected, myitem SelectedItem) { }}
2.
@EActivity (r.layout.my_list) public class Mylistactivity extends Activity { //... @ItemClick public void mylistitemclicked (int position) {//position ID } @ItemLongClick public Void mylistitemlongclicked (int position) { } @ItemSelect public void Mylistitemselected (Boolean selected, int position) { }}
@SeekBarProgressChange
Equivalent to SeekBar.OnSeekBarChangeListener.onProgressChanged (SeekBar, int, Boolean)
@SeekBarProgressChange (R.id.seekbar) void Onprogresschangeonseekbar (SeekBar seekBar, int progress, Boolean fromuser) { //Something here} @SeekBarProgressChange (R.id.seekbar) void Onprogresschangeonseekbar (SeekBar seekBar, int Progress) { //Something here} @SeekBarProgressChange ({r.id.seekbar1, r.id.seekbar2}) void Onprogresschangeonseekbar (SeekBar SeekBar) { //Something here} @SeekBarProgressChange ({r.id.seekbar1, R.ID.SEEKBAR2}) void Onprogresschangeonseekbar () { //Something here} @SeekBarTouchStart and @SeekBarTouchStop
@SeekBarTouchStart and @SeekBarTouchStop listening for start and end events
@TextChange
@TextChange (R.id.hellotextview) void Ontextchangesonhellotextview (charsequence text, TextView hello, int before, int Start, int count) { //Something here} @TextChange void hellotextviewtextchanged (TextView hello) { //Something H ere} @TextChange ({r.id.edittext, r.id.hellotextview}) void Ontextchangesonsometextviews (TextView TV, charsequence Text) { //Something here} @TextChange (R.id.hellotextview) void Ontextchangesonhellotextview () { //Something here}
@BeforeTextChange
@BeforeTextChange (R.id.hellotextview) void Beforetextchangedonhellotextview (TextView hello, charsequence text, int Start, int count, int after) { //Something here} @BeforeTextChange void hellotextviewbeforetextchanged (TextView hell O) { //Something here} @BeforeTextChange ({r.id.edittext, r.id.hellotextview}) void Beforetextchangedonsometextviews (TextView TV, charsequence text) { //Something here} @BeforeTextChange ( R.id.hellotextview) void Beforetextchangedonhellotextview () { //Something here}
@AfterTextChange
@AfterTextChange (R.id.hellotextview) void Aftertextchangedonhellotextview (Editable text, TextView hello) { // Something here} @AfterTextChange void hellotextviewaftertextchanged (TextView hello) { //Something here} @AfterText Change ({r.id.edittext, r.id.hellotextview}) void Aftertextchangedonsometextviews (TextView TV, Editable text) { // Something here} @AfterTextChange (R.id.hellotextview) void Aftertextchangedonhellotextview () { //Something here}
@OptionsMenu and Optionsitem
@EActivity @optionsmenu (r.menu.my_menu) public class MyActivity extends Activity { @OptionMenuItem MenuItem Menusearch; @OptionsItem (r.id.menushare) void MyMethod () { //can specify the ID in the annotation, or use the naming conv Ention } @OptionsItem void homeselected () { //home is selected in the Action Bar //The "selected" K Eyword is optional } @OptionsItem boolean menusearch () { menusearch.setvisible (false); Menusearch was selected //The return type is void or Boolean (False to allow normal menu processing to Procee D, true to consume it here) return true; @OptionsItem ({r.id.menu_search, r.id.menu_delete}) void Multiplemenuitems () { //You can specify multiple menu item IDs in @OptionsItem } @OptionsItem void Menu_add (MenuItem item) { //can add a MenuItem parameter to access it }}
Or:
@EActivity @optionsmenu ({r.menu.my_menu1, r.menu.my_menu2}) public class MyActivity extends Activity {}
@Background Run:
void MyMethod () { somebackgroundwork ("Hello", 42);} @Backgroundvoid somebackgroundwork (String aparam, long Anotherparam) { [...]}
Cancel:
void MyMethod () { somecancellablebackground ("Hello", "a"); [...] Boolean mayinterruptifrunning = true; Backgroundexecutor.cancelall ("Cancellable_task", mayinterruptifrunning);} @Background (id= "Cancellable_task") void Somecancellablebackground (String aparam, long Anotherparam) { [...]}
Non-concurrent operation:
void MyMethod () {for (int i = 0; i <; i++) Somesequentialbackgroundmethod (i);} @Background (serial = "test") void Somesequentialbackgroundmethod (int i) { systemclock.sleep (new Random (). Nextint ( ) +1000); LOG.D ("AA", "Value:" + i);}
Delay:
@Background (delay=2000) void Doinbackgroundaftertwoseconds () {}
@UiThreadUI Thread:
void MyMethod () { doinuithread ("Hello", 42);} @UiThreadvoid Doinuithread (String aparam, long Anotherparam) { [...]}
Delay:
@UiThread (delay=2000) void Doinuithreadaftertwoseconds () {}
To optimize the UI thread:
@UiThread (propagation = propagation.reuse) void Runinsamethreadifonuithread () {}
Change in progress value:
@EActivitypublic class MyActivity extends Activity { @Background void Dosomestuffinbackground () { Publishprogress (0); Do some stuff publishprogress (ten); Do some stuff publishprogress (); } @UiThread void publishprogress (int progress) { //Update Progress views }}
@OnActivityResult
@OnActivityResult (Request_code) void Onresult (int resultcode, Intent data) {} @OnActivityResult (request_code) void Onresult (int resultcode) {} @OnActivityResult (another_request_code) void Onresult (Intent data) {} @OnActivityResult ( Another_request_code) void Onresult () {}
The above method of gaze usage basically includes the event binding in an ordinary program. Use the Androidannotations framework to focus on logic development. The main thing is to simplify code writing. Easy maintenance.
If you have any questions, you can refer to the official document Https://github.com/excilys/androidannotations/wiki/Cookbook, or leave a message. Reprint must indicate the source.
Original link: http://www.bkjia.com/Androidjc/868503.html
Android's hottest high-speed development framework Androidannotations use specific explanations