您的位置:首页 > 移动开发 > Android开发

androidannotations摘要

2015-12-17 22:12 429 查看

Activity

@EActivity(R.layout.main)
public class MyActivity extends Activity {

}

//在AndroidManifest.xml中这样注册activity
<activity android:name=".MyListActivity_" />


—— startActivity

startActivity(this, MyListActivity_.class);

MyListActivity_.intent(context).start();

Intent intent = MyListActivity_.intent(context).get();

MyListActivity_.intent(context).flags(FLAG_ACTIVITY_CLEAR_TOP).start();//以指定启动方式启动

MyListActivity_.intent(context).myDateExtra(someDate).start();//传递数据

MyListActivity_.intent(context).startForResult(REQUEST_CODE);

MyListActivity_.intent(context).withOptions(bundle).start();

MyListActivity_.intent(context).start().withAnimation(enterAnimRes, exitAnimRes));//打开动画启动

@OnActivityResult(REQUEST_CODE)
void onResult(int resultCode) {
}


Services

@EService
public class MyService extends IntentService {
}


MyService_.intent(getApplication()).start();

Intent intent = MyService_.intent(context).build();

MyService_.intent(getApplication()).stop();//停止services

MyService_.intent(context).flags(Intent.FLAG_GRANT_READ_URI_PERMISSION).start();

IntentServices

MyIntentService_.intent(getApplication()) .myAction(“test”) .start();

@EIntentService
public class MyIntentService extends IntentService {

public MyIntentService() {
super("MyIntentService");
}

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

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

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


Application

@EApplication
public class MyApplication extends Application
}


@EBean//普通类
public class MyBean {
@App//application变量的声明
MyApplication application;
}


Fragment

<fragment
android:id="@+id/myFragment"
android:name="com.company.MyFragment_"//指定name属性
android:layout_width="fill_parent"
android:layout_height="fill_parent" />


@EFragment(R.layout.my_fragment_layout)//这里的要用上面指定的name
public class MyFragment extends Fragment {

@FragmentById(R.id.myFragment)
MyFragment myFragment2;

@FragmentByTag("myFragmentTag")
MyFragment myFragmentTag2;
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return null;//当复写oncreateview时应该return null
}
}


MyFragment fragment = new MyFragment_();

MyFragment fragment = MyFragment_.builder().build();

普通类 class

@EBean
public class MyClass {
@Bean//其它类的引用
MyOtherClass dependency;
public MyClass() {
// notificationManager and dependency are null
}
}


——-单例设计模式

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

}


Receiver

@EReceiver
public class MyIntentService extends BroadcastReceiver {

@ReceiverAction("BROADCAST_ACTION_NAME")
void mySimpleAction(Intent intent) {
// ...
}

@ReceiverAction
void myAction(@ReceiverAction.Extra String valueString, Context context) {
// ...
}

@ReceiverAction
void anotherAction(@ReceiverAction.Extra("specialExtraName") String valueString, @ReceiverAction.Extra long valueLong) {//Parcelable 和Serializable
// ...
}

@Override
public void onReceive(Context context, Intent intent) {
// empty, will be overridden in generated subclass
}
}


View

@EView
public class CustomButton extends Button {
public CustomButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
}


//在xml中这样使用

<com.androidannotations.view.CustomButton_
android:layout_width="match_parent"
android:layout_height="wrap_content" />


CustomButton button = CustomButton_.build(context);

ViewGroup

<?xml version="1.0" encoding="utf-8"?>
<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/check" />

<TextView
android:id="@+id/title"
android:layout_toLeftOf="@+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="12pt" />

<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="10pt" />

</merge>


//自定义组合控件

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

}


//使用

<com.androidannotations.viewgroup.TitleWithSubtitle_
android:id="@+id/firstTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content" />


ContentProvider

@EProvider
public class MyContentProvider extends ContentProvider {
}


Injection

@ViewById

@ViewById(R.id.myTextView)
TextView textView;


@AfterViews

//这个可替代activity中的initView,操作ui要在这个方法中,不能在oncreate(),

void updateTextWithDate() {
myTextView.setText("Date: " + new Date());
}


@AfterInject

//在类中声明的其它的构造函数调用后,还没有注射字段这之间做的事

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


@ViewsById

//用于list和view

@ViewsById({R.id.myTextView1, R.id.myOtherTextView})
List<TextView> textViews;


@AfterExtras

//在注射额外代码之后执行

@AfterExtras
public void doSomethingAfterExtrasInjection() {
// someExtra and anotherExtra are set to the value contained in the incoming intent
// if an intent does not contain one of the extra values the field remains unchanged
}


@SystemService

@SystemService
NotificationManager notificationManager;


@RootContext

@RootContext
Context context;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: