androidannotations是一款Android注入框架,可以方便我们编程,减少代码量(变相减少了错误的可能),让我们可以更多的把精力放在逻辑处理上。
本文API介绍取至https://github.com/excilys/androidannotations/wiki/Cookbook
特征
配置
在AS中,需要在项目的build.gradle中进行加入如下配置(有注释部分)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| apply plugin: 'com.android.application' apply plugin: 'android-apt'
buildscript { repositories { mavenCentral() } dependencies { classpath 'com.android.tools.build:gradle:1.0.0' // 使用android-apt classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4' } }
repositories { mavenCentral() mavenLocal() }
dependencies { def AAVersion = '3.2' // 设置版本 apt "org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion" }
apt { arguments { androidManifestFile variant.outputs[0].processResources.manifestFile resourcePackageName 'com.myproject.name' // 指定包名 } }
|
使用
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| @EActivity(R.layout.translate) public class TranslateActivity extends Activity { @ViewById EditText textInput;
@ViewById(R.id.myTextView) TextView result;
@AnimationRes Animation fadeIn;
@Click void doTranslate() { translateInBackground(textInput.getText().toString()); }
@Background void translateInBackground(String textToTranslate) { String translatedText = callGoogleTranslate(textToTranslate); showResult(translatedText); }
@UiThread void showResult(String translatedText) { result.setText(translatedText); result.startAnimation(fadeIn); }
}
|
Activity
1 2 3 4
| @EActivity(R.layout.main) public class MyActivity extends Activity { }
|
也可以不注入
1 2 3 4 5 6 7 8
| @EActivity public class MyListActivity extends ListActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); } }
|
注:使用Androidannotations注入的Activity在AndroidManifest.xml中配置时,名字后需要加上_,如MainActivity则为.MainActivity_
Fragment
1 2 3 4
| @EFragment public class MyFragment extends Fragment { }
|
在布局中需要使用MyFragment_表示
1 2 3 4 5 6 7 8 9 10 11 12
| <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>
|
在程序中使用
1
| MyFragment fragment = new MyFragment_();
|
也可以注入布局
1 2 3 4
| @EFragment(R.layout.my_custom_layout) public class MyFragment extends ListFragment { }
|
获取Fragment时
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @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; }
|
自定义控件
1 2 3 4 5 6 7 8 9 10
| @EView public class CustomButton extends Button { @App MyApplication application;
@StringRes public CustomButton(Context context,AttributeSet attrs) { super.(context,attrs); } }
|
在布局中使用时
1 2 3 4 5
| <com.myapp.view.CustomButton_ android:layout_width="match_parent" android:layout_height="wrap_content" />
|
自定义ViewGroups
layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| <?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>
|
Java代码
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| @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); } }
|
使用
1 2 3 4 5
| <com.myapp.viewgroup.TitleWithSubtitle_ android:id="@+id/firstTitle" android:layout_width="match_parent" android:layout_height="wrap_content" />
|
Service
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| @EService public class MyService extends IntentService { @SystemService NotificationManager notificationManager; @Bean MyEnhancedDatastore datastore;
@RestService MyRestClient = myRestClient;
@OrmLiteDao(helper = DatabaseHelper.class,model = User.class) UserDao userDao;
public MyService() { super(MyService.class.getSimpleName); }
@Override proteced void onHandleIntent(Intent intent){ showTast(); }
@UiThread void showToast() { Toast.makeText(getApplicationContext(),"Hi~",Toast.LENGTH_LONG).show(); } }
|
对于IntentService还可以设置一些操作
1 2 3 4
| @ServiceAction void mySimpleAction(String param) { }
|
通过MyIntentService_.intent(getApplication()).myAction("test").start()使用
获取资源
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| @StringRes(R.string.hello) String myHelloString; @StringRes String hello;
@ColorRes(R.color.backgroundColor) int someColor; @ColorRes int backgoundColor;
@AnimationRes(R.anim.fadein) Animation fadein; @AnimationRes Animation fadein;
@DimensionRes(R.dimen.fontsize) float fontSizeDimension; @DimensionRes float fontsize;
@DimensionPixelOffsetRes(R.sting.fontsize) int fontSizeDimension; @DimensionPixelOffsetRes int fontsize;
|
其他
- @BooleanRes
- @ColorStateListRes
- @DrawableRes
- @IntArrayRes
- @IntegerRes
- @LayoutRes
- @MovieRes
- @TextRes
- @TextArrayRes
- @StringArrayRes
Intent
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
| @EActivity public class MyActivity extends Activity { @Extra("myStringExtra") String myMessage; @Extra("myDateExtra") Date myDate = new Date();
@Override protected void onNewIntent(Intent intent) { }
@AfterExtras public void doSomethingAfterExtrasInjection() { } }
|