其实我觉得并没有什么卵用,为了了解依赖注入学习下这个。
1. 在project/build.gradle中添加以下内容:
buildscript {
repositories {
jcenter()
//注解
mavenCentral()
}
dependencies {
classpath 'com.android.tools.build:gradle:2.3.2'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
classpath 'com.neenbedankt.gradle.plugins:android-apt:1.4'//注解
}
}
allprojects {
repositories {
jcenter()
//注解
mavenCentral()
mavenLocal()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
2. 在app/build.gradle中添加以下内容:
apply plugin: 'com.android.application'
//注解 begin
apply plugin: 'android-apt'
def AAVersion = '3.3.1'
//注解 end
android {
compileSdkVersion 26
buildToolsVersion '25.0.2'
defaultConfig {
applicationId "com.baron.ch"
minSdkVersion 19
targetSdkVersion 26
versionCode 1
versionName "1.0"
vectorDrawables.useSupportLibrary = true
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
//注解 begin
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile
}
}
//注解 end
dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:25.3.1'
compile 'com.android.support:design:26.+'
compile 'com.android.support:support-vector-drawable:26.+'
compile 'com.android.support.constraint:constraint-layout:1.0.2'
compile 'com.android.support:support-v4:26.+'
//注解 begin
apt "org.androidannotations:androidannotations:$AAVersion"
compile "org.androidannotations:androidannotations-api:$AAVersion"
//注解 end
}
3. 在AndroidManifest.xml中的所有activity名称后面添加下划线
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity_">
<intent-filter>
<action android:name="android.intent.action.MAIN"/>
<category android:name="android.intent.category.LAUNCHER"/>
</intent-filter>
</activity>
</application>
添加后会变红,不用管。然后编译,能通过就没问题了。
4. 使用
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.Fullscreen;
import org.androidannotations.annotations.LongClick;
import org.androidannotations.annotations.NoTitle;
import org.androidannotations.annotations.ViewById;
@NoTitle
@Fullscreen
@EActivity(R.layout.activity_test_aat)
public class TestAatActivity extends AppCompatActivity {
@ViewById
Button btnOk;
@ViewById
Button btnCancel;
@ViewById
TextView tv_testaat;
@Click({R.id.btn_ok, R.id.btn_cancel})
public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_ok:
tv_testaat.setText("hello aat ok");
break;
case R.id.btn_cancel:
tv_testaat.setText("hello aat cancel");
break;
}
}
@LongClick({R.id.btn_cancel})
public void longClick(View view) {
/*int id = view.getId();
switch (id) {
case R.id.btn_cancel:
tv_testaat.setText("btn cancel made a long click");
break;
}*/
tv_testaat.setText("btn cancel made a long click");
}
@AfterViews
public void init(){
Log.e("tag","init");
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.e("tag", "onCreate");
//可省略!
//setContentView(R.layout.activity_my);
//tv_testaat.setText("hello"); 报错,空指针异常
//因为在onCreate()被调用的时候,@ViewById还没有被set,也就是都为null
//所以如果你要对组件进行一定的初始化,那么你要用@AfterViews注解
}
@Override
protected void onResume() {
super.onResume();
Log.e("tag","onResume");
}
@Override
protected void onStart() {
super.onStart();
Log.e("tag","onStart");
}
}
参考连接:https://github.com/androidannotations/androidannotations/wiki