AndroidAnnotations框架
AndroidAnnotations配置
- 首先在全局gradle文件中添加如下信息
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral() //添加maven中心仓库
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.2.0-beta1‘
classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘ //添加加载地址
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
repositories {
mavenCentral() //添加maven中心仓库
mavenLocal() //添加本地maven仓库
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
- 然后在项目app的gradle中添加如下信息:
apply plugin: ‘com.android.application‘
apply plugin: ‘android-apt‘ //需要添加
def AAVersion = ‘3.3.2‘ //需要添加,可根据当前版本自定义其中的版本号
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.xhb.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
}
}
}
dependencies {
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, {
exclude group: ‘com.android.support‘, module: ‘support-annotations‘
})
compile ‘com.android.support:appcompat-v7:23.4.0‘
testCompile ‘junit:junit:4.12‘
apt "org.androidannotations:androidannotations:$AAVersion" //需要添加
compile "org.androidannotations:androidannotations-api:$AAVersion" //需要添加
}
apt { //整个内容需要添加
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
}
}
- 最后构建时如果速度缓慢,需要自备FQ,之后在配置清单文件中对Activity的命名进行改变,在最后面加上一个下划线_,如果报错的话就进行编译一下就可以了
AndroidAnnotations使用
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
jcenter()
mavenCentral() //添加maven中心仓库
}
dependencies {
classpath ‘com.android.tools.build:gradle:2.2.0-beta1‘
classpath ‘com.neenbedankt.gradle.plugins:android-apt:1.8‘ //添加加载地址
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
repositories {
mavenCentral() //添加maven中心仓库
mavenLocal() //添加本地maven仓库
}
allprojects {
repositories {
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
apply plugin: ‘com.android.application‘
apply plugin: ‘android-apt‘ //需要添加
def AAVersion = ‘3.3.2‘ //需要添加,可根据当前版本自定义其中的版本号
android {
compileSdkVersion 23
buildToolsVersion "23.0.3"
defaultConfig {
applicationId "com.xhb.app"
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile(‘proguard-android.txt‘), ‘proguard-rules.pro‘
}
}
}
dependencies {
compile fileTree(dir: ‘libs‘, include: [‘*.jar‘])
androidTestCompile(‘com.android.support.test.espresso:espresso-core:2.2.2‘, {
exclude group: ‘com.android.support‘, module: ‘support-annotations‘
})
compile ‘com.android.support:appcompat-v7:23.4.0‘
testCompile ‘junit:junit:4.12‘
apt "org.androidannotations:androidannotations:$AAVersion" //需要添加
compile "org.androidannotations:androidannotations-api:$AAVersion" //需要添加
}
apt { //整个内容需要添加
arguments {
androidManifestFile variant.outputs[0]?.processResources?.manifestFile
}
}
需要在AndroidManifest.xml中将<activity android:name=".MainActivity">
置换为<activity android:name=".MainActivity_">
注解
@EActivity(R.layout.activity_main)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@ViewById(R.id.turn_next)
Button btn;
Button btn;
btn=(Button)findViewById(R.id.turn_next);
@ViewsById({R.id.textView,R.id.textView2,R.id.textView3})
List<TextView> list;
List<TextView> list=new ArrayList<TextView>;
list.add(findViewById(R.id.textView));
list.add(findViewById(R.id.textView2));
list.add(findViewById(R.id.textView3));
@Click(R.id.turn_next)
public void startActivity(){
startActivity(new Intent(MainActivity.this,TestActivity_.class));
}
findViewById(R.id.turn_next).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
startActivity(new Intent(MainActivity.this,TestActivity_.class));
}
});
@Click({R.id.textView2,R.id.textView3})
public void show(){
Toast.makeText(getApplicationContext(),”Helloworld”,Toast.LENGTH_SHORT).show();
}
findViewById(R.id.textView2).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { Toast.makeText(getApplicationContext(),"Helloworld",Toast.LENGTH_SHORT).show();
}
});
findViewById(R.id.textView3).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) { Toast.makeText(getApplicationContext(),"Helloworld",Toast.LENGTH_SHORT).show();
}
});
常用的注解事件
@AfterInject 定义的方法在类的构造方法执行后执行
@AfterTextChange定义的方法在TextView及其子类的Text属性改变后执行
@AfterViews 定义的方法在setContentView后执行
@Background //开启新线程后台运行,注意不要引用UI控件,而且返回值类型一定是void
@UiThread 定义的方法在主线程执行
@UiThread(delay=2000) //可以设置延时时间,以毫秒为单位
@BeforeTextChange 定义的方法在TextView及其子类的Text属性改变前执行
@Click //事件控制,可以以按钮的id作为方法名,同时支持的事件还有onLongClick,onTextChange等
@EActivity //布局文件在这里声明,不用在setContentView
@EProvider 在 ContentProvider中启用Annotations
@EReceive 在BroadcastReceiver中启用Annotations
@EService 在Service中启用Annotations
@EView 在自定义的View的子类中启用Annotations
@Fullscreen 全屏
@NoTitle 无标题栏
@ViewById //控件这样标注,由于是IOC模式,因此不需要自己实例化
@ViewById(R.id.myTextView) //提供id来生成控件,如果不指定ID,默认以控件名进行查找,如上面的myEditText
@StringRes(R.string.hello) //String资源
@DimensionRes(R.dimen.activity_horizontal_margin)//dimen资源
@Extra(MainActivity.NAME_KEY) //取Intent传递的值
@WindowFeature({Window.FEATURE_NO_TITLE,Window.FEATURE_INDETERMINATE_PROGRESS})