AndroidAnnotations学习笔记(二)

[size=large][color=red][b]@EBean[/b][/color][/size]


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

}


[size=large][color=red][b]@Bean[/b][/color][/size]
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;

}


[size=large][color=red][b]@EService[/b][/color][/size]

示例代码

@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();


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

当然 你也能结束这个Service


MusicService_.intent(getApplication()).stop();


[size=large][color=red][b]@EIntentService[/b][/color][/size]

异步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();



[size=large][color=red][b]@EReceiver[/b][/color][/size]

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

示例代码


@EReceiver
public class MyReceiver extends BroadcastReceiver {
@SystemService
NotificationManager notificationManager;

@Bean
SomeObject someObject;
}



[size=large][color=red][b]@ReceiverAction[/b][/color][/size]

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

}


[size=large][color=red][b]@Receiver[/b][/color][/size]

你在 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();
}
}



[size=large][color=red][b]@EProvider[/b][/color][/size]

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


[size=large][color=red][b]@EView[/b][/color][/size]


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


[size=large][color=red][b]@EViewGroup[/b][/color][/size]

这里,我们先定义这个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>


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

然后我们在这个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.");
}
}


[size=large][color=red][b]@EFragment[/b][/color][/size]

在一个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>


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

当然我们也可以通过代码的方式使用这个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 {
}


[size=large][color=red][b]@FragmentById、@FragmentByTag[/b][/color][/size]

你可以在@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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值