博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android 最火高速开发框架AndroidAnnotations使用具体解释
阅读量:5117 次
发布时间:2019-06-13

本文共 13092 字,大约阅读时间需要 43 分钟。

     文章中有eclipse配置步骤,文章中的简介,本篇注重解说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_();

普通类:

@EBeanpublic class MyClass {
}

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

Activity中使用:

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

也能够用来声明接口:

@Bean(MyImplementation.class)    MyInterface myInterface;

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

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

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

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

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

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

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

@EView

@EViewpublic 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

@EApplicationpublic class MyApplication extends Application {
}
Activity中使用:
@EActivitypublic class MyActivity extends Activity {
@App MyApplication application;}
@EService
@EServicepublic class MyService extends Service {
}
跳转service:
MyService_.intent(getApplication()).start();
停止service:
MyService_.intent(getApplication()).stop();
@EReceiver
@EReceiverpublic class MyReceiver extends BroadcastReceiver {
}
@Receiver
能够替代声明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,变量名称必须和布局的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
@EActivitypublic class MyActivity extends Activity {
@StringRes(R.string.hello) String myHelloString;//不能设置成私有变量 @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;}
其它的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();}
或者:
@EActivitypublic class MyActivity extends Activity {
// The name of the extra will be "myMessage",名字必须一致 @Extra String myMessage;}
传值:
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 {//必须用在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 {//等同于   @NonConfigurationInstance  Bitmap someBitmap;  @NonConfigurationInstance  @Bean  MyBackgroundTask myBackgroundTask;}
@HttpsClient
 
@HttpsClientHttpClient httpsClient;
演示样例:
@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 {//等同于   @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() {//假设不指定则函数名和id相应 [...]}@Clickvoid yetAnotherButton(View clickedView) {
[...]}
其它点击事件:
  •  with @Click
  •  with @LongClick
  •  with @Touch

AdapterViewEvents 

  •  with @ItemClick
  •  with @ItemLongClick
  •  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 和 @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);}@Backgroundvoid 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() {
}
@UiThread
UI线程:
void myMethod() {
doInUiThread("hello", 42);}@UiThreadvoid doInUiThread(String aParam, long anotherParam) {
[...]}
延迟:
@UiThread(delay=2000)void doInUiThreadAfterTwoSeconds() {
}
优化UI线程:
@UiThread(propagation = Propagation.REUSE)void runInSameThreadIfOnUiThread() {
}
进度值改变:
@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() {
}
以上的凝视使用方法基本包括了寻常程序中的事件绑定,用AndroidAnnotations框架能够专注于做逻辑开发,最主要是简化代码编写,easy维护。
如有问题能够參考官方文档,
或者留言。转载务必注明出处。

转载于:https://www.cnblogs.com/zfyouxi/p/4188005.html

你可能感兴趣的文章
如何给a标签绑定ajax事件
查看>>
花瓣网图片爬取
查看>>
网络营销相关缩写名称CPM CPT CPC CPA CPS SEM SEO解析
查看>>
微信、陌陌阴影下,中国社交领域未来的机会在哪里,四个象限看社交的格局...
查看>>
go语言,golang学习笔记1 官网下载安装,中文社区,开发工具LiteIDE
查看>>
Python开发WebService:REST,web.py,eurasia,Django
查看>>
图像处理——图像增强
查看>>
hdu 2051
查看>>
Qt 之 pro 配置详解
查看>>
Xcode的一些使用技巧
查看>>
PHPCMS 标签与示例
查看>>
JavaScript: 认识 Object、原型、原型链与继承。
查看>>
性能测试系列学习 day1
查看>>
Linux服务器开启tomcat的gc日志
查看>>
PCL—点云滤波(基于点云频率) 低层次点云处理
查看>>
Flask框架视图多层装饰器问题
查看>>
VS2005新建应用工程出错解决方法
查看>>
自制Informatica教程
查看>>
Day03 单行函数
查看>>
MongoDB 安全配置
查看>>