Android开发之注解框架的使用(android annotations)

来源:互联网 发布:电子书管理类软件 编辑:程序博客网 时间:2024/05/21 10:39

我们首先讲讲使用注解开发有什么好处呢?

1.减少代码量,无需重复写类似代码

2.无需写handler+Thread处理超时操作

3.使代码简洁,规范,看起来方便


常用的注解:

@EActivity(R.layout.main)
引入布局layout

@ViewById(R.id.myInput)
EditText  myInput;
找到相应的控件Id

@Click({R.id.button1,R.id.button2,R.id.button3}) 
doClickThing(){

}
对对应的控件设置点击事件

@AfterView 
void init(){

}
加载界面完成后做的操作

@UiThread
处理耗时操作

@UiTread(delay=2)
handler 延迟操作

app的build.grade文件内容

<span style="font-size:18px;">buildscript {    repositories {        mavenCentral()    }    dependencies {        // replace with the current version of the Android plugin        classpath 'com.android.tools.build:gradle:1.2.3'        // the latest version of the android-apt plugin        classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'    }}apply plugin: 'com.android.application'apply plugin: 'com.neenbedankt.android-apt'android {    compileSdkVersion 21    buildToolsVersion "21.1.2"    defaultConfig {        applicationId "你的包名"        minSdkVersion 16        targetSdkVersion 21        versionCode 15        versionName "0.1.5"    }    buildTypes {        release {            minifyEnabled false            proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'        }    }    repositories {        flatDir {            dirs 'libs'        }    }}dependencies {    compile fileTree(include: ['*.jar'], dir: 'libs')    compile 'com.android.support:support-v4:21.0.3'    apt 'org.androidannotations:androidannotations:3.2'    compile 'org.androidannotations:androidannotations-api:3.2'}apt {    arguments {        androidManifestFile variant.outputs[0].processResources.manifestFile        resourcePackageName android.defaultConfig.applicationId    }}</span>


0 0
原创粉丝点击