Android's most popular quick development framework: AndroidAnnotations

Source: Internet
Author: User

Android's most popular quick development framework: AndroidAnnotations

 

The Android fastest development framework androidannotations contains the eclipse configuration steps in this article. The Android fastest development framework AndroidAnnotations briefly introduces this article. This article focuses on the use of annotation methods in AndroidAnnotations.

@ EActivity

Example:

@EActivity(R.layout.main)public class MyActivity extends Activity {}
@ Fragment

Example:

 
@EFragment(R.layout.my_fragment_layout) public class MyFragment extends Fragment { }
Registration:

Create:

MyFragment fragment = new MyFragment_();

Common class:

@EBeanpublic class MyClass {}

 

Note: This class must have only one constructor, and the parameter can have at most one context.

Use in Activity:

@EActivitypublic class MyActivity extends Activity {  @Bean  MyOtherClass myOtherClass;}

It can also be used to declare the interface:

@Bean(MyImplementation.class)    MyInterface myInterface;

You can also inject the root environment into common classes:

@EBeanpublic class MyClass {  @RootContext  Context context;  // Only injected if the root context is an activity  @RootContext  Activity activity;  // Only injected if the root context is a service  @RootContext  Service service;  // Only injected if the root context is an instance of MyActivity  @RootContext  MyActivity myActivity;}

If you want to perform some operations during the class creation period, you can:

@AfterInject  public void doSomethingAfterInjection() {    // notificationManager and dependency are set  }

The following declaration is required for the singleton class:

@EBean(scope = Scope.Singleton)public class MySingleton {}

Note: view and event binding cannot be injected into the singleton class, because the lifecycle of a singleton is longer than that of Activity and Service to avoid memory overflow.

@ EView

@EViewpublic class CustomButton extends Button {        @App        MyApplication application;        @StringRes        String someStringResource;    public CustomButton(Context context, AttributeSet attrs) {        super(context, attrs);    }}

 

Registration:

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);    }}

Registration:

@ EApplication
@EApplicationpublic class MyApplication extends Application {}
Use in Activity:
@EActivitypublic class MyActivity extends Activity {  @App  MyApplication application;}

@ EService
@EServicepublic class MyService extends Service {}

Jump to 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 be consistent with the layout id name @ 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() {
// Make sure to set the view here. Do not set it in oncreate () because the view has not been injected during oncreate () execution.
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 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 {// It 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 {// equivalent to Activity. onRetainNonConfigurationInstance () @ NonConfigurationInstance Bitmap someBitmap; @ NonConfigurationInstance @ Bean MyBackgroundTask myBackgroundTask ;}

    @ HttpsClient
    @HttpsClient HttpClient httpsClient;

    Example:
    @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 {// equivalent to Fragment Argument @ FragmentArg (response) String myMessage; @ FragmentArg String response; @ FragmentArg (myDateExtra) Date response = new Date ();}
    MyFragment myFragment = MyFragment_.builder()  .myMessage(Hello)  .anotherStringArgument(World)  .build();

    @ Click
    @ Click (R. id. myButton) void myButtonWasClicked () {[...]} @ Clickvoid anotherButton () {// if not specified, the function name and id correspond to [...]} @ 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
    Item selection with @ ItemSelect can be called in two ways: 1.

    @ EActivity (R. layout. my_list) public class MyListActivity extends Activity {//... @ ItemClick public void myListItemClicked (MyItem clickedItem) {// MyItem is the entity class of the adapter, which is equivalent to the 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) {// location 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 accept listener 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 Here } @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 hello) {    // 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 } @AfterTextChange({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() {          // You can specify the ID in the annotation, or use the naming convention        }    @OptionsItem    void homeSelected() {      // home was selected in the action bar          // The Selected keyword is optional    }    @OptionsItem    boolean menuSearch() {          menuSearch.setVisible(false);          // menuSearch was selected          // the return type may be void or boolean (false to allow normal menu processing to proceed, 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) {      // You 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:
    void myMethod() {    someBackgroundWork(hello, 42);}@Backgroundvoid someBackgroundWork(String aParam, long anotherParam) {    [...]}

    Cancel:
    void myMethod() {    someCancellableBackground(hello, 42);    [...]    boolean mayInterruptIfRunning = true;    BackgroundExecutor.cancelAll(cancellable_task, mayInterruptIfRunning);}@Background(id=cancellable_task)void someCancellableBackground(String aParam, long anotherParam) {    [...]}

    Non-concurrent execution:
    void myMethod() {    for (int i = 0; i < 10; i++)        someSequentialBackgroundMethod(i);}@Background(serial = test)void someSequentialBackgroundMethod(int i) {    SystemClock.sleep(new Random().nextInt(2000)+1000);    Log.d(AA, value :  + i);}

    Latency:
    @Background(delay=2000)void doInBackgroundAfterTwoSeconds() {}

    @ UiThreadUI thread:
    void myMethod() {    doInUiThread(hello, 42);}@UiThreadvoid doInUiThread(String aParam, long anotherParam) {    [...]}

    Latency:
    @UiThread(delay=2000)void doInUiThreadAfterTwoSeconds() {}

    UI thread optimization:
    @UiThread(propagation = Propagation.REUSE)void runInSameThreadIfOnUiThread() {}

    Progress value change:
    @EActivitypublic class MyActivity extends Activity {  @Background  void doSomeStuffInBackground() {    publishProgress(0);    // Do some stuff    publishProgress(10);    // Do some stuff    publishProgress(100);  }  @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() { }

     

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.