一、在android studio中使用android annotation
在project工程文件下的build.gradle文件中
buildscript {
repositories {
jcenter ()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.3.0'
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4+'
}
}
allprojects {
repositories {
jcenter ()
}
}
在app文件下的build.gradle文件中
apply plugin: 'com.android.application'
apply plugin: 'android-apt'
def AAVersion='3.2+'
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.study.radasm.annotationdemo"
minSdkVersion 17
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.2.0'
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
// if you have multiple outputs (when using splits), you may want to have other index than 0
// You can set optional annotation processing options here, like these commented options:
// logLevel 'INFO'
// logFile '/var/log/aa.log'
}
}
出现问题请科学上网
!
二、加强组件
Activities相关
1、@EActivity
该注解的参数必须是一个layout文件的id,该文件将用作该actiivty的Content View
你同样可以传递一个空的参数,这代表该activity不需要设置Content View,或者你希望在绑定之前,自己设置一个Content View。
栗子:
@EActivity(R.layout.main)
public class MyActivity extends Activity {
}
没有layout id:
@EActivity
public class MyListActivity extends ListActivity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
}
Fragment相关
1、对FragmentActivity的支持
当你使用FragmentActivity来代替Activiy的时候,使用EActivity
不会有任何问题。
2、支持Fragment
AndroidAnnotation支持android.app.Fragment
和android.support.v4.app.Fragment
,并会根据使用的不同自动进行切换。
3、加强Fragments
使用EFragment
@EFragment
public class MyFragment extends Fragment {
}
AndroidAnnotation会自动生成类MyFragment_
。这样,在你的xml文件中,你需要使用生成的类的名称。
<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>
代码书写:
MyFragment fragment = new MyFragment_();
或者使用流式buider:
MyFragment fragment = MyFragment_.builder().build();
现在,你能在Fragment中使用各种各样的annotation:
@EFragment
public class MyFragment extends Fragment {
@Bean
SomeBean someBean;
@ViewById
TextView myTextView;
@App
MyApplication customApplication;
@SystemService
ActivityManager activityManager;
@OrmLiteDao(helper = DatabaseHelper.class, model = User.class)
UserDao userDao;
@Click
void myButton() {
}
@UiThread
void uiThread() {
}
@AfterInject
void calledAfterInjection() {
}
@AfterViews
void calledAfterViewInjection() {
}
@Receiver(actions = "org.androidannotations.ACTION_1")
protected void onAction1() {
}
}
View的注入和事件监听只能基于在Fragment内部的View。但请注意到,这并非
@EBean
被注入到fragments内部的原因,真正的原因是:They hava access to the activity views。
4、Fragment的布局文件
标准的写法是覆写onCreateView()
方法:
@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;
}
}
或者和EActivity
一样,给@EFragment
传递一个参数:
@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
}
如果你需要通过覆写onCreateView()方法来获得savedinstanceState
,你仍旧可以通过让该方法返回null
来让androidannotation去安置该Fragment的layout文件。
@EFragment(R.layout.my_fragment_layout)
public class MyFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return null;
}
}
5、强制进行layout注入
从AndroidAnnotation 3.3开始
在某些情况下,比方说你的Fragment继承自ListFragment
,这样,子类将会返回一个在onCreateView()
中返回一个非空的view。这样你的注解可能就不起作用了,这样,forceLayoutInjection就应运而生了,如果被设置为true,则无论何种情况,注解均起作用,而默认的值为false。
@EFragment(R.layout.my_custom_layout)
public class MyFragment extends ListFragment {
// R.layout.my_custom_layout will be injected
}
6、Fragments注入
或许你用了@EActivity
,@EFragment
,@EView
,@EViewGroup
,@EBean
,使用@FragmentById
或者@FragmentByTag
。如果你不做任何特殊的参数传递,那么默认成员名称就是参数。
我们强烈的推荐使用
@FragmentById
而不是@FragmentByTag
.
请注意@FragmentById
和@FragmentByTag
只有在Fragment中才能进行注入。我们不能对Fragment进行创建,这些需要进行注入的Fragment必须早就已经在Activity中,或者在onCreate()
方法中进行了创建。
允许你在没有进行
@EFragment
注入的fragments进行相关行为的注入。
@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;
}
7、DialogFragment
s中的一些注意点
很不幸的是,如果一个Fragment使用了@EFragment
注入,那么你将不能在onCreateDialog()
中创建一个新的Dialog
对象。你只需调用super.onCreateDialog()
方法,该方法中返回的Dialog
的对象将为你所用。在这之后,你可以使用通过@AfterViews
注解的方法来继续调用初始化你的Views。
你可以参考这里以了解更多!