@EBean
@EBean
public class MyClass {
}
注意:这个类必须仅仅只能有一个构造函数,参数最多有一个context。
你可以在@EBean标注的类里使用其他注解
@EBean
public class MyClass {
@SystemService
NotificationManager notificationManager;
@UiThread
void updateUI() {
}
}
使用与界面相关的的注解
@EBean
public class MyClass {
@ViewById(R.id.tv_test)
TextView myTextView;
@Click(R.id.btOk)
void handleButtonClick() {
}
}
还可以注入根环境
@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
TestActivity myActivity;
}
如果想在类创建时期做一些操作可以这样写
@EBean
public class MyClass {
@AfterInject
public void doSomethingAfterInjection() {
// notificationManager and dependency are set
}
}
Scopes参数
这个参数有两种
default:每次都注入
singleton:只注入一次,单例的
@EBean(scope = Scope.Singleton)
public class MyClass {
}
@Bean
Activity中使用这个类
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
@Bean
MyClass myclass;
}
也可以注入到接口
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
@Bean
MyClass myclass;
@Bean(MyImplementation.class)
myInterface myinterface;
}
@EService
示例代码
@EService
public class MusicService extends Service{
}
在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。
@EService
public class MusicService extends Service{
@SystemService
NotificationManager notificationManager;
@RestService
MyRestClient myRestClient;
@UiThread
void showToast() {
Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();
}
}
当你要运行这个Service是用如下代码
MusicService_.intent(getApplication()).extra("op", op).start();
注意那个“_”,调用的时候不能写你类的名称,而是要在类名后面加上“_”
当然 你也能结束这个Service
MusicService_.intent(getApplication()).stop();
@EIntentService
异步Service的注解,当然在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。
@EIntentService
public class MyIntentService extends IntentService {
@ServiceAction
void mySimpleAction() {
// ...
}
@ServiceAction
void myAction(String param) {
// ...
}
@Override
protected void onHandleIntent(Intent intent) {
// Do nothing here
}
}
启动方式
MyIntentService.intent(getApplication()) //
.myAction("test") //
.start();
@EReceiver
对于Android BroadcastReceiver类你可以使用这个注解,当然在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。
示例代码
@EReceiver
public class MyReceiver extends BroadcastReceiver {
@SystemService
NotificationManager notificationManager;
@Bean
SomeObject someObject;
}
@ReceiverAction
@ReceiverAction 简单的广播接收器,对接收到的消息进行处理
示例代码
@EReceiver
public class MyReceiver extends BroadcastReceiver {
@ReceiverAction("BROADCAST_ACTION_NAME")
void mySimpleAction(Intent intent) {
}
@ReceiverAction
void myAction(@ReceiverAction.Extra String valueString, Context context) {
}
@ReceiverAction
void anotherAction(Context context, @ReceiverAction.Extra("specialExtraName") String valueString, @ReceiverAction.Extra long valueLong) {
Toast.makeText(context, "string:"+valueString + "<->" + valueLong, 1000).show();
}
@Override
public void onReceive(Context context, Intent intent) {
// TODO Auto-generated method stub
String action = intent.getAction();
Toast.makeText(context, "静态:"+action, 1000).show();
}
}
@Receiver
你在 activity/fragment/service 中可以直接使用@Receiver,而不需要定义一个BroadcastReceiver
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity { {
@Receiver(actions = android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED)
protected void onAction1() {
Toast.makeText(this, "show ", Toast.LENGTH_LONG).show();
}
}
@EProvider
在Android Content Provider类中使用这个注解
@EProvider
public class MyContentProvider extends ContentProvider {
@SystemService
NotificationManager notificationManager;
@UiThread
void showToast() {
Toast.makeText(getContext().getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();
}
}
@EView
@EView
public class CustomButton extends Button {
@App
MyApplication application;
@StringRes
String someStringResource;
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}
定义之后,我们就能在我们的布局文件中使用这个View了,但值得注意的是类名后面工加上“_”
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.androidannotations.view.CustomButton_
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- ... -->
</LinearLayout>
当然我们也可以通过代码的方式使用这个View
CustomButton button = CustomButton_.build(context);
@EViewGroup
这里,我们先定义这个ViewGroup的布局文件
<merge xmlns:android="http://schemas.android.com/apk/res/android" >
<ImageView
android:id="@+id/image"
android:layout_alignParentRight="true"
android:layout_alignBottom="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="@drawable/ic_launcher" />
<TextView
android:id="@+id/title"
android:layout_toLeftOf="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="6pt" />
<TextView
android:id="@+id/subtitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/title"
android:textColor="#FFdedede"
android:textSize="5pt" />
</merge>
然后使用@EViewGroup注解开发ViewGroup类
@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);
}
}
这样,我们的自定义的ViewGroup类的完成了,我们可以这样使用这个类
在我们要使用这个ViewGroup的View布局文件中,增加如下代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.hista.weiweilove.TestActivity" >
<com.hista.weiweilove.bean.TitleWithSubtitle_
android:id="@+id/firstTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
不知道你注意到没有,在这个ViewGroup的类名后同样增加了“_”
然后我们在这个Activity类中写如下代码
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity { {
@ViewById
protected TitleWithSubtitle firstTitle;
@AfterViews
void afterView(){
firstTitle.setTexts("decouple your code",
"Hide the component logic from the code using it.");
}
}
@EFragment
在一个Fragment上使用androidannotations 注解,使用@EFragment
@EFragment
public class MyFragment extends Fragment {
}
当然,我们在这个类中也可以使用其他的androidannotations 注解
我们可以这样使用这个类,在我们要使用这个Fragment的View布局文件中,增加如下代码
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<fragment
android:id="@+id/myFragment"
android:name="com.company.MyFragment_"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
不知道你注意到没有,在这个Fragment的类名后同样增加了“_”
当然我们也可以通过代码的方式使用这个Fragment
MyFragment fragment = new MyFragment_();
让Fragment与一个布局绑定的标准方式是这样的
@EFragment
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
}
当然使用@EFragment注解后我们可以这样写:
@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
}
@FragmentById、@FragmentByTag
你可以在@EActivity, @EFragment, @EView, @EViewGroup, @EBean,中使用@FragmentById or @FragmentByTag这两个注解。如果你不指定注解的值,他将使用你定义的字段的名称。
官网推荐使用 @FragmentById,不推荐使用@FragmentByTag。
请注意,@FragmentById and @FragmentByTag 只是使用,而不是去创建,所以你们必须在布局或代码里定义出来。
@EActivity(R.layout.fragments)
public class MyFragmentActivity extends FragmentActivity {
@FragmentById
MyFragment myFragment;
@FragmentById(R.id.myFragment)
MyFragment myFragment2;
@FragmentByTag
MyFragment myFragmentTag;
@FragmentByTag("myFragmentTag")
MyFragment myFragmentTag2;
}