- 浏览: 17844 次
- 性别:
- 来自: 沈阳
-
文章分类
最新评论
@EBean
注意:这个类必须仅仅只能有一个构造函数,参数最多有一个context。
你可以在@EBean标注的类里使用其他注解
使用与界面相关的的注解
还可以注入根环境
如果想在类创建时期做一些操作可以这样写
Scopes参数
这个参数有两种
default:每次都注入
singleton:只注入一次,单例的
@Bean
Activity中使用这个类
也可以注入到接口
@EService
示例代码
在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。
当你要运行这个Service是用如下代码
注意那个“_”,调用的时候不能写你类的名称,而是要在类名后面加上“_”
当然 你也能结束这个Service
@EIntentService
异步Service的注解,当然在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。
启动方式
@EReceiver
对于Android BroadcastReceiver类你可以使用这个注解,当然在这个类里你也可以使用其他的绝大多数的AndroidAnnotations 注解。
示例代码
@ReceiverAction
@ReceiverAction 简单的广播接收器,对接收到的消息进行处理
示例代码
@Receiver
你在 activity/fragment/service 中可以直接使用@Receiver,而不需要定义一个BroadcastReceiver
@EProvider
在Android Content Provider类中使用这个注解
@EView
定义之后,我们就能在我们的布局文件中使用这个View了,但值得注意的是类名后面工加上“_”
当然我们也可以通过代码的方式使用这个View
@EViewGroup
这里,我们先定义这个ViewGroup的布局文件
然后使用@EViewGroup注解开发ViewGroup类
这样,我们的自定义的ViewGroup类的完成了,我们可以这样使用这个类
在我们要使用这个ViewGroup的View布局文件中,增加如下代码
不知道你注意到没有,在这个ViewGroup的类名后同样增加了“_”
然后我们在这个Activity类中写如下代码
@EFragment
在一个Fragment上使用androidannotations 注解,使用@EFragment
当然,我们在这个类中也可以使用其他的androidannotations 注解
我们可以这样使用这个类,在我们要使用这个Fragment的View布局文件中,增加如下代码
不知道你注意到没有,在这个Fragment的类名后同样增加了“_”
当然我们也可以通过代码的方式使用这个Fragment
让Fragment与一个布局绑定的标准方式是这样的
当然使用@EFragment注解后我们可以这样写:
@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;
}
@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;
}
发表评论
-
AndroidAnnotations学习笔记(七)续
2015-01-29 16:39 0@InstanceState -
AndroidAnnotations学习笔记(七)
2015-01-29 15:04 1681@InstanceState 在onSaveInstance ... -
AndroidAnnotations学习笔记--资源(六)
2015-01-29 09:09 4862所有@XXXRes 注解都是你的res文件夹所对应的Andro ... -
AndroidAnnotations学习笔记--线程(五)
2015-01-28 14:43 1539@Background 这个注解表明,这个方法将运行现UI线程 ... -
AndroidAnnotations学习笔记--事件续(四)
2015-01-28 10:11 2197@OptionsMenu、@OptionsMenuItem、@ ... -
AndroidAnnotations学习笔记--事件(三)
2015-01-28 08:50 2191@TextChange 这个注解是用于接收 android.t ... -
AndroidAnnotations学习笔记(一)
2015-01-27 12:56 2671@EActivity 原来在没用(AndroidAnnotat ...
相关推荐
Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习笔记Java学习...
希沃白板学习笔记.pdf希沃白板学习笔记.pdf希沃白板学习笔记.pdf希沃白板学习笔记.pdf希沃白板学习笔记.pdf希沃白板学习笔记.pdf希沃白板学习笔记.pdf希沃白板学习笔记.pdf希沃白板学习笔记.pdf
Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Springcloud学习笔记.md,Spring...
2022吴恩达机器学习笔记汇总(共10章节).zip2022吴恩达机器学习笔记汇总(共10章节).zip2022吴恩达机器学习笔记汇总(共10章节).zip2022吴恩达机器学习笔记汇总(共10章节).zip2022吴恩达机器学习笔记汇总(共10章节).zip...
CCNA学习笔记 CCNA学习笔记 CCNA学习笔记
人工智能学习笔记,人工智能学习笔记,人工智能学习笔记人工智能学习笔记,人工智能学习笔记,人工智能学习笔记人工智能学习笔记,人工智能学习笔记,人工智能学习笔记人工智能学习笔记,人工智能学习笔记,人工智能...
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
云的学习笔记-云的学习笔记系统-云的学习笔记系统源码-云的学习笔记管理系统-云的学习笔记管理系统java代码-云的学习笔记系统设计与实现-基于ssm的云的学习笔记系统-基于Web的云的学习笔记系统设计与实现-云的学习...
nginx学习笔记(软件+学习笔记) 仅供学习交流! 后续会持续分享相关资源,记得关注哦! nginx学习笔记(软件+学习笔记) 仅供学习交流! 后续会持续分享相关资源,记得关注哦! nginx学习笔记(软件+学习笔记) ...
"Android学习笔记" Android学习笔记是Android开发者的必读书籍,书中涵盖了Android系统架构、Activity、Intent、资源管理等多方面的知识。本笔记对应的学习资源《第一行代码》是Android开发者的入门必读书籍,书中...
docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,docker学习笔记,...
PHP个人学习笔记
ssh学习笔记1 ssh学习笔记1 ssh学习笔记1 ssh学习笔记1 ssh学习笔记1 ssh学习笔记1 ssh学习笔记1
Contiki学习笔记:进程、事件、etimer关系 Contiki 实例: Contiki学习笔记:创建两个交互进程 Contiki 主函数剖析: Contiki学习笔记:main函数剖析 Contiki学习笔记:启动一个进程process_start Contiki学习笔记...