AndroidAnnotations学习笔记(二)

本文详细介绍了AndroidAnnotations框架的各种注解及其应用场景,包括@EBean、@EActivity、@EService等,帮助开发者更好地理解和使用该框架。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

用到了fragment的相关知识


转自http://hista.iteye.com/blog/2180405


@EBean

Java代码  收藏代码
  1. @EBean  
  2. public class MyClass {  
  3.   
  4. }  
@EBean
public class MyClass {

}

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

你可以在@EBean标注的类里使用其他注解
Java代码  收藏代码
  1. @EBean  
  2. public class MyClass {  
  3.       
  4.     @SystemService  
  5.     NotificationManager notificationManager;  
  6.   
  7.     @UiThread  
  8.     void updateUI() {  
  9.           
  10.     }  
  11.   
  12. }  
@EBean
public class MyClass {
	
	@SystemService
	NotificationManager notificationManager;

	@UiThread
	void updateUI() {
		
	}

}


使用与界面相关的的注解
Java代码  收藏代码
  1. @EBean  
  2. public class MyClass {  
  3.       
  4.     @ViewById(R.id.tv_test)  
  5.     TextView myTextView;  
  6.   
  7.     @Click(R.id.btOk)  
  8.     void handleButtonClick() {  
  9.           
  10.     }  
  11.   
  12. }  
@EBean
public class MyClass {
	
	@ViewById(R.id.tv_test)
	TextView myTextView;

	@Click(R.id.btOk)
	void handleButtonClick() {
		
	}

}


还可以注入根环境
Java代码  收藏代码
  1. @EBean  
  2. public class MyClass {  
  3.       
  4.     @RootContext  
  5.     Context context;  
  6.   
  7.     // Only injected if the root context is an activity  
  8.     @RootContext  
  9.     Activity activity;  
  10.   
  11.     // Only injected if the root context is a service  
  12.     @RootContext  
  13.     Service service;  
  14.   
  15.     // Only injected if the root context is an instance of MyActivity  
  16.     @RootContext  
  17.     TestActivity myActivity;  
  18.   
  19. }  
@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;

}


如果想在类创建时期做一些操作可以这样写
Java代码  收藏代码
  1. @EBean  
  2. public class MyClass {  
  3.       
  4.     @AfterInject  
  5.     public void doSomethingAfterInjection() {  
  6.         // notificationManager and dependency are set  
  7.     }  
  8.   
  9. }  
@EBean
public class MyClass {
	
	@AfterInject
	public void doSomethingAfterInjection() {
		// notificationManager and dependency are set
	}

}


Scopes参数
这个参数有两种
default:每次都注入
singleton:只注入一次,单例的
Java代码  收藏代码
  1. @EBean(scope = Scope.Singleton)  
  2. public class MyClass {  
  3.   
  4. }  
@EBean(scope = Scope.Singleton)
public class MyClass {

}


@Bean
Activity中使用这个类
Java代码  收藏代码
  1. @EActivity(R.layout.activity_test)  
  2. public class TestActivity extends Activity {  
  3.       
  4.     @Bean  
  5.     MyClass myclass;  
  6.   
  7. }  
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
	
	@Bean
	MyClass myclass;

}


也可以注入到接口
Java代码  收藏代码
  1. @EActivity(R.layout.activity_test)  
  2. public class TestActivity extends Activity {  
  3.       
  4.     @Bean  
  5.     MyClass myclass;  
  6.       
  7.     @Bean(MyImplementation.class)  
  8.     myInterface myinterface;  
  9.   
  10. }  
@EActivity(R.layout.activity_test)
public class TestActivity extends Activity {
	
	@Bean
	MyClass myclass;
	
	@Bean(MyImplementation.class)
	myInterface myinterface;

}


@EService

示例代码
Java代码  收藏代码
  1. @EService  
  2. public class MusicService extends Service{  
  3. }  
@EService
public class MusicService extends Service{
}


在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。

Java代码  收藏代码
  1. @EService  
  2. public class MusicService extends Service{  
  3.     @SystemService  
  4.     NotificationManager notificationManager;  
  5.       
  6.     @RestService  
  7.     MyRestClient myRestClient;  
  8.       
  9.     @UiThread  
  10.     void showToast() {  
  11.         Toast.makeText(getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();  
  12.     }  
  13.       
  14. }  
@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是用如下代码

Java代码  收藏代码
  1. MusicService_.intent(getApplication()).extra("op", op).start();  
MusicService_.intent(getApplication()).extra("op", op).start();


注意那个“_”,调用的时候不能写你类的名称,而是要在类名后面加上“_”

当然 你也能结束这个Service

Java代码  收藏代码
  1. MusicService_.intent(getApplication()).stop();  
MusicService_.intent(getApplication()).stop();


@EIntentService

异步Service的注解,当然在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。

Java代码  收藏代码
  1. @EIntentService  
  2. public class MyIntentService extends IntentService {  
  3.   
  4.     @ServiceAction  
  5.     void mySimpleAction() {  
  6.         // ...  
  7.     }  
  8.   
  9.     @ServiceAction  
  10.     void myAction(String param) {  
  11.         // ...  
  12.     }  
  13.   
  14.     @Override  
  15.     protected void onHandleIntent(Intent intent) {  
  16.         // Do nothing here  
  17.     }  
  18. }  
@EIntentService
public class MyIntentService extends IntentService {

    @ServiceAction
    void mySimpleAction() {
        // ...
    }

    @ServiceAction
    void myAction(String param) {
        // ...
    }

    @Override
    protected void onHandleIntent(Intent intent) {
        // Do nothing here
    }
}


启动方式

Java代码  收藏代码
  1. MyIntentService.intent(getApplication()) //  
  2.     .myAction("test"//  
  3.     .start();  
MyIntentService.intent(getApplication()) //
    .myAction("test") //
    .start();



@EReceiver

对于Android BroadcastReceiver类你可以使用这个注解,当然在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。

示例代码

Java代码  收藏代码
  1. @EReceiver  
  2. public class MyReceiver extends BroadcastReceiver {  
  3.     @SystemService  
  4.     NotificationManager notificationManager;  
  5.       
  6.     @Bean  
  7.     SomeObject someObject;  
  8. }  
@EReceiver
public class MyReceiver extends BroadcastReceiver {
	@SystemService
  	NotificationManager notificationManager;
  	
  	@Bean
	SomeObject someObject;
}



@ReceiverAction

@ReceiverAction 简单的广播接收器,对接收到的消息进行处理

示例代码

Java代码  收藏代码
  1. @EReceiver  
  2. public class MyReceiver extends BroadcastReceiver {  
  3.   
  4.     @ReceiverAction("BROADCAST_ACTION_NAME")  
  5.     void mySimpleAction(Intent intent) {  
  6.           
  7.     }  
  8.       
  9.     @ReceiverAction  
  10.     void myAction(@ReceiverAction.Extra String valueString, Context context) {  
  11.     }  
  12.       
  13.     @ReceiverAction  
  14.     void anotherAction(Context context, @ReceiverAction.Extra("specialExtraName") String valueString, @ReceiverAction.Extra long valueLong) {  
  15.         Toast.makeText(context, "string:"+valueString + "<->" + valueLong, 1000).show();  
  16.     }  
  17.       
  18.     @Override  
  19.     public void onReceive(Context context, Intent intent) {  
  20.         // TODO Auto-generated method stub  
  21.         String action = intent.getAction();  
  22.         Toast.makeText(context, "静态:"+action, 1000).show();  
  23.     }  
  24.   
  25. }  
@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

Java代码  收藏代码
  1. @EActivity(R.layout.activity_test)  
  2. public class TestActivity extends Activity { {  
  3.       
  4.     @Receiver(actions = android.bluetooth.BluetoothAdapter.ACTION_STATE_CHANGED)  
  5.     protected void onAction1() {  
  6.         Toast.makeText(this"show ", Toast.LENGTH_LONG).show();  
  7.     }  
  8. }  
@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类中使用这个注解

Java代码  收藏代码
  1. @EProvider  
  2. public class MyContentProvider extends ContentProvider {  
  3.   
  4.     @SystemService  
  5.     NotificationManager notificationManager;  
  6.   
  7.     @UiThread  
  8.     void showToast() {  
  9.         Toast.makeText(getContext().getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();  
  10.     }  
  11. }  
@EProvider
public class MyContentProvider extends ContentProvider {

	@SystemService
	NotificationManager notificationManager;

	@UiThread
	void showToast() {
		Toast.makeText(getContext().getApplicationContext(), "Hello World!", Toast.LENGTH_LONG).show();
	}
}


@EView

Java代码  收藏代码
  1. @EView  
  2. public class CustomButton extends Button {  
  3.   
  4.         @App  
  5.         MyApplication application;  
  6.   
  7.         @StringRes  
  8.         String someStringResource;  
  9.   
  10.     public CustomButton(Context context, AttributeSet attrs) {  
  11.         super(context, attrs);  
  12.     }  
  13. }  
@EView
public class CustomButton extends Button {

        @App
        MyApplication application;

        @StringRes
        String someStringResource;

    public CustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
    }
}


定义之后,我们就能在我们的布局文件中使用这个View了,但值得注意的是类名后面工加上“_”

Xml代码  收藏代码
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <com.androidannotations.view.CustomButton_  
  8.         android:layout_width="match_parent"  
  9.         android:layout_height="wrap_content" />  
  10.   
  11.     <!-- ... -->  
  12.   
  13. </LinearLayout>  


当然我们也可以通过代码的方式使用这个View

Java代码  收藏代码
  1. CustomButton button = CustomButton_.build(context);  
CustomButton button = CustomButton_.build(context);


@EViewGroup

这里,我们先定义这个ViewGroup的布局文件

Xml代码  收藏代码
  1. <merge xmlns:android="http://schemas.android.com/apk/res/android" >  
  2.   
  3.     <ImageView  
  4.         android:id="@+id/image"  
  5.         android:layout_alignParentRight="true"  
  6.         android:layout_alignBottom="@+id/title"  
  7.         android:layout_width="wrap_content"  
  8.         android:layout_height="wrap_content"  
  9.         android:src="@drawable/ic_launcher" />  
  10.   
  11.     <TextView  
  12.         android:id="@+id/title"  
  13.         android:layout_toLeftOf="@+id/image"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content"  
  16.         android:textSize="6pt" />  
  17.   
  18.     <TextView  
  19.         android:id="@+id/subtitle"  
  20.         android:layout_width="match_parent"  
  21.         android:layout_height="wrap_content"  
  22.         android:layout_below="@+id/title"  
  23.         android:textColor="#FFdedede"  
  24.         android:textSize="5pt" />  
  25.   
  26. </merge>  


然后使用@EViewGroup注解开发ViewGroup类

Java代码  收藏代码
  1. @EViewGroup(R.layout.title_with_subtitle)  
  2. public class TitleWithSubtitle extends RelativeLayout {  
  3.   
  4.     @ViewById  
  5.     protected TextView title, subtitle;  
  6.   
  7.     public TitleWithSubtitle(Context context, AttributeSet attrs) {  
  8.         super(context, attrs);  
  9.     }  
  10.   
  11.     public void setTexts(String titleText, String subTitleText) {  
  12.         title.setText(titleText);  
  13.         subtitle.setText(subTitleText);  
  14.     }  
  15.   
  16. }  
@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布局文件中,增加如下代码

Xml代码  收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical"  
  6.     android:paddingBottom="@dimen/activity_vertical_margin"  
  7.     android:paddingLeft="@dimen/activity_horizontal_margin"  
  8.     android:paddingRight="@dimen/activity_horizontal_margin"  
  9.     android:paddingTop="@dimen/activity_vertical_margin"  
  10.     tools:context="com.hista.weiweilove.TestActivity" >  
  11.       
  12.     <com.hista.weiweilove.bean.TitleWithSubtitle_  
  13.         android:id="@+id/firstTitle"  
  14.         android:layout_width="match_parent"  
  15.         android:layout_height="wrap_content" />  
  16.           
  17. </LinearLayout>  


不知道你注意到没有,在这个ViewGroup的类名后同样增加了“_”

然后我们在这个Activity类中写如下代码

Java代码  收藏代码
  1. @EActivity(R.layout.activity_test)  
  2. public class TestActivity extends Activity { {  
  3.       
  4.     @ViewById  
  5.     protected TitleWithSubtitle firstTitle;  
  6.       
  7.     @AfterViews  
  8.     void afterView(){  
  9.           
  10.         firstTitle.setTexts("decouple your code",  
  11.                 "Hide the component logic from the code using it.");  
  12.     }  
  13. }  
@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

Java代码  收藏代码
  1. @EFragment  
  2. public class MyFragment extends Fragment {  
  3.   
  4. }  
@EFragment
public class MyFragment extends Fragment {

}

当然,我们在这个类中也可以使用其他的androidannotations 注解

我们可以这样使用这个类,在我们要使用这个Fragment的View布局文件中,增加如下代码

Xml代码  收藏代码
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     android:layout_width="fill_parent"  
  3.     android:layout_height="fill_parent"  
  4.     android:orientation="horizontal" >  
  5.   
  6.     <fragment  
  7.         android:id="@+id/myFragment"  
  8.         android:name="com.company.MyFragment_"  
  9.         android:layout_width="fill_parent"  
  10.         android:layout_height="fill_parent" />  
  11.   
  12. </LinearLayout>  


不知道你注意到没有,在这个Fragment的类名后同样增加了“_”

当然我们也可以通过代码的方式使用这个Fragment

Java代码  收藏代码
  1. MyFragment fragment = new MyFragment_();  
MyFragment fragment = new MyFragment_();


让Fragment与一个布局绑定的标准方式是这样的

Java代码  收藏代码
  1. @EFragment  
  2. public class MyFragment extends Fragment {  
  3.     public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {  
  4.         View view = inflater.inflate(R.layout.my_fragment_layout, container, false);  
  5.         return view;  
  6.     }  
  7. }  
@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注解后我们可以这样写:

Java代码  收藏代码
  1. @EFragment(R.layout.my_fragment_layout)  
  2. public class MyFragment extends Fragment {  
  3. }  
@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;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值