Android 最火的高速开发框架androidannotations配置具体解释文章中有eclipse配置步骤。Android 最火高速开发框架AndroidAnnotations简介文章中的简介。本篇注重解说AndroidAnnotations中注解方法的使用。

@EActivity

演示样例:

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

@fragment

演示样例:

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

注冊:

创建:

MyFragment fragment = new MyFragment_();

普通类:

@EBean
public class MyClass { }

注意:这个类必须只只能有一个构造函数。參数最多有一个context。

Activity中使用:

@EActivity
public class MyActivity extends Activity { @Bean
MyOtherClass myOtherClass; }

也能够用来声明接口:

@Bean(MyImplementation.class)
MyInterface myInterface;

在普通类中还能够注入根环境:

@EBean
public 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; }

假设想在类创建时期做一些操作能够:

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

单例类须要例如以下声明:

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

注意:在单例类里面不能够注入view和事件绑定,由于单例的生命周期比Activity和Service的要长,以免发生内存溢出。

@EView

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

注冊:

创建:

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

注冊:

@EApplication

@EApplication
public class MyApplication extends Application { }

Activity中使用:

@EActivity
public class MyActivity extends Activity { @App
MyApplication application; }

@EService

@EService
public class MyService extends Service { }

跳转service:

MyService_.intent(getApplication()).start();

停止service:

MyService_.intent(getApplication()).stop();

@EReceiver

@EReceiver
public class MyReceiver extends BroadcastReceiver { }

@Receiver能够替代声明BroadcastReceiver

@EActivity
public class MyActivity extends Activity { @Receiver(actions = "org.androidannotations.ACTION_1")
protected void onAction1() { } }

@EProvider

@EProvider
public class MyContentProvider extends ContentProvider { }

@ViewById

@EActivity
public class MyActivity extends Activity { // Injects R.id.myEditText,变量名称必须和布局的id名称一致
@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() {
//一定要在这里进行view的一些设置。不要在oncreate()中设置,由于oncreate()在运行时 view还没有注入

myTextView.setText("Date: " + new Date()); }[...]

@StringRes

@EActivity
public class MyActivity extends Activity { @StringRes(R.string.hello)
String myHelloString;//不能设置成私有变量 @StringRes
String hello; }

@ColorRes

@EActivity
public class MyActivity extends Activity { @ColorRes(R.color.backgroundColor)
int someColor; @ColorRes
int backgroundColor; }

@AnimationRes

@EActivity
public class MyActivity extends Activity { @AnimationRes(R.anim.fadein)
XmlResourceParser xmlResAnim; @AnimationRes
Animation fadein; }

@DimensionRes

@EActivity
public class MyActivity extends Activity { @DimensionRes(R.dimen.fontsize)
float fontSizeDimension; @DimensionRes
float fontsize; }

@DImensionPixelOffsetRes

@EActivity
public class MyActivity extends Activity { @DimensionPixelOffsetRes(R.string.fontsize)
int fontSizeDimension; @DimensionPixelOffsetRes
int fontsize; }

@DimensionPixelSizeRes

@EActivity
public class MyActivity extends Activity { @DimensionPixelSizeRes(R.string.fontsize)
int fontSizeDimension; @DimensionPixelSizeRes
int fontsize; }

其它的Res:

  • @BooleanRes
  • @ColorStateListRes
  • @DrawableRes
  • @IntArrayRes
  • @IntegerRes
  • @LayoutRes
  • @MovieRes
  • @TextRes
  • @TextArrayRes
  • @StringArrayRes
  • @Extra
@EActivity
public class MyActivity extends Activity { @Extra("myStringExtra")
String myMessage; @Extra("myDateExtra")
Date myDateExtraWithDefaultValue = new Date(); }

或者:

@EActivity
public class MyActivity extends Activity { // The name of the extra will be "myMessage",名字必须一致
@Extra
String myMessage;
}

传值:

MyActivity_.intent().myMessage("hello").start() ;

@SystemService

@EActivity
public class MyActivity extends Activity {//
@SystemService
NotificationManager notificationManager; }

@HtmlRes

@EActivity
public 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

@EActivity
public class MyActivity extends Activity {//必须用在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 {//等同于 Activity.onRetainNonConfigurationInstance()

  @NonConfigurationInstance
Bitmap someBitmap; @NonConfigurationInstance
@Bean
MyBackgroundTask myBackgroundTask; }

@HttpsClient

@HttpsClient
HttpClient httpsClient;

演示样例:

@EActivity
public 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

@EFragment
public class MyFragment extends Fragment {//等同于 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() {
[...]
}
@Click
void anotherButton() {//假设不指定则函数名和id相应
[...]
}
@Click
void yetAnotherButton(View clickedView) {
[...]
}

其它点击事件:

Clicks with @Click

Long clicks with @LongClick

Touches with @Touch



AdapterViewEvents

Item clicks with @ItemClick

Long item clicks with @ItemLongClick

Item selection with @ItemSelect 有两种方式调用: 1.

@EActivity(R.layout.my_list)
public class MyListActivity extends Activity { // ... @ItemClick
public void myListItemClicked(MyItem clickedItem) {//MyItem是adapter的实体类,等同于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) {//位置id } @ItemLongClick
public void myListItemLongClicked(int position) { } @ItemSelect
public void myListItemSelected(boolean selected, int position) { } }

@SeekBarProgressChange

//等同于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 和 @SeekBarTouchStop接受開始和结束事件的监听

@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和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
}
}

或者:

@EActivity
@OptionsMenu({R.menu.my_menu1, R.menu.my_menu2})
public class MyActivity extends Activity { }

@Background运行:

void myMethod() {
someBackgroundWork("hello", 42);
} @Background
void someBackgroundWork(String aParam, long anotherParam) {
[...]
}

取消:

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

非并发运行:

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

延迟:

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

@UiThreadUI线程:

void myMethod() {
doInUiThread("hello", 42);
} @UiThread
void doInUiThread(String aParam, long anotherParam) {
[...]
}

延迟:

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

优化UI线程:

@UiThread(propagation = Propagation.REUSE)
void runInSameThreadIfOnUiThread() {
}

进度值改变:

@EActivity
public 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() {
}

以上的凝视使用方法基本包括了寻常程序中的事件绑定。用AndroidAnnotations框架能够专注于做逻辑开发。最主要是简化代码编写。easy维护。

如有问题能够參考官方文档https://github.com/excilys/androidannotations/wiki/Cookbook,或者留言。转载务必注明出处。

原文链接:http://www.bkjia.com/Androidjc/868503.html

Android 最火的高速开发框架AndroidAnnotations使用具体解释的更多相关文章

  1. Android 最火高速开发框架AndroidAnnotations使用具体解释

    Android 最火的高速开发框架androidannotations配置具体解释文章中有eclipse配置步骤,Android 最火高速开发框架AndroidAnnotations简介文章中的简介, ...

  2. Android 最火的高速开发框架xUtils

    Github下载地址:https://github.com/wyouflf/xUtils xUtils简单介绍 xUtils 包括了非常多有用的Android工具. xUtils 最初源于Afinal ...

  3. Android 最火高速开发框架AndroidAnnotations简单介绍

    在上一篇Android 最火的高速开发框架androidannotations配置具体解释中介绍了在eclipse中配置androidannotation的步骤,如需配置请參考. 1.目标 andro ...

  4. Android 最火框架XUtils之注解机制具体解释

    在上一篇文章Android 最火的高速开发框架XUtils中简介了xUtils的基本用法,这篇文章说一下xUtils里面的注解原理. 先来看一下xUtils里面demo的代码: @ViewInject ...

  5. 9款Android经常使用的高速开发框架

    1.Afinal框架 项目地址:https://github.com/yangfuhai/afinal 项目地址:http://www.oschina.net/p/afinal 主要有四大模块:  ( ...

  6. 【转】Android 最火的快速开发框架XUtils

    原文:http://blog.csdn.net/rain_butterfly/article/details/37812371 最近搜了一些框架供初学者学习,比较了一下XUtils是目前git上比较活 ...

  7. Android 最火的快速开发框架XUtils

    参考:http://www.oschina.net/p/xutils 项目git地址https://github.com/wyouflf/xUtils 目录(?)[-] 最近搜了一些框架供初学者学习比 ...

  8. 【转】Android 最火框架XUtils之注解机制详解

    原文:http://blog.csdn.net/rain_butterfly/article/details/37931031 在上一篇文章Android 最火的快速开发框架XUtils中简单介绍了x ...

  9. Android开发框架androidannotations的使用

    Android开发框架AndroidAnnotations,它除了有依赖注入的特性以外,还集成了Ormlite,Spring-android中的REST模板.使用起来非常方便,大大提高了开发效率. 使 ...

随机推荐

  1. OpenCASCADE7.3.0 is available for download

    OpenCASCADE7.3.0 is available for download OPEN CASCADE is pleased to announce a new public release ...

  2. 版本号控制-搭建gitserver

    GitHub是一个免费托管开源码的Gitserver,假设我们不想公开项目的源码,又不想付费使用.那么我们能够自己搭建一台Gitserver. 以下我们就看看,怎样在Ubuntu上搭建Gitserve ...

  3. Spring 配置文件头部xmls解析

    <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w ...

  4. A股借壳上市?暂时没戏

    近期股市波动,让不少P2P概念股打回原型.同花顺数据显示,上周五P2P概念股整体下跌9.28%,其中除了5家上市公司停牌外,14家P2P概念股跌幅都超过了9%.此前大热的多伦股份自6月23日以来,股价 ...

  5. js中Object.defineProperties 定义一个在原对象可读可写的方法

    function A(){ this.name = 'hellow word'; } Object.defineProperties( A.prototype,{ doSomething2 : { v ...

  6. OpenCV —— 矩阵和图像操作

    cvAbs , cvAbsDiff , cvAbsDiffS cvAdd , cvAddS , cvAddWeighted(可添加权重) #include <cv.h> #include ...

  7. 原生JS实现页面内定位

    需求:点击跳转到页面指定位置 <div id="test">点击跳转到此处</div> [法一]: 利用a标签的锚点跳转 <a href=" ...

  8. 获取Spring容器中Bean实例的工具类(Java泛型方法实现)

    在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...

  9. 【Uva 10163】Storage Keepers

    [Link]: [Description] 你有n(n≤100)个相同的仓库.有m(m≤30)个人应聘守卫,第i个应聘者的能力值 为Pi(1≤Pi≤1000).每个仓库只能有一个守卫,但一个守卫可以看 ...

  10. android图像处理(3)浮雕效果

    这篇将讲到图片特效处理的浮雕效果.跟前面一样是对像素点进行处理,算法是通用的. 算法原理:用前一个像素点的RGB值分别减去当前像素点的RGB值并加上127作为当前像素点的RGB值. 例: ABC 求B ...