我自己对Annotations框架理解是在Android 编码过程中一些操作用注解的方式代替,从而提高编码效率和代码可读性
在网上看了很多个版本的配置 攻略,网上配置的可能版本比较旧,终于能用了~~~
Android Studio 需要配置 两个文件
Project:~下的是全局配置文件,:(红色为添加内容)
buildscript {
repositories {
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:1.2.3'
// 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()
}
}
Module:app是局部配置文件:
apply plugin: 'com.android.application'
<span style="color:#ff0000;">apply plugin: 'android-apt'
</span>
android {
compileSdkVersion 22
buildToolsVersion "22.0.1"
defaultConfig {
applicationId "com.example.administrator.androidappanontations"
minSdkVersion 19
targetSdkVersion 22
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
apt "org.androidannotations:androidannotations:3.2" <span style="font-family: Arial, Helvetica, sans-serif;">//添加</span>
compile fileTree(dir: 'libs', include: ['*.jar'])
compile 'com.android.support:appcompat-v7:22.0.0' <span style="font-family: Arial, Helvetica, sans-serif;">//添加</span>
compile "org.androidannotations:androidannotations-api:3.2" <span style="font-family: Arial, Helvetica, sans-serif;">//添加</span>
}
apt {
arguments {
androidManifestFile variant.outputs[0].processResources.manifestFile //添加
//androidManifestFile variant.processResources.manifestFile //旧版的Android Studio 用这一句替换上一句
resourcePackageName "com.example.administrator.androidappanontations"//添加此处填写你自己的包名
}
}
配置文件使用Activity必须使用在Activit后面加_
<activity android:name=".SecondActivity_" android:label="@string/app_name" />Activity跳转在要跳转的Activity也必须加~
Intent i=new Intent(MainActivity.this,SecondActivity_.class);
修改完后要重新编译一遍文件(ctrl+F9)
然后就可以使用Annotations了;
1:使用
@EActivity(R.layout.activity_main)
来代替
setContentView(R.layout.activity_main);设置布局文件
2:
(这里很重要,定义view的时候一定不能用private ,使用注解的方法也一定不能用 private)
使用
@ViewById(R.id.button) Button button;
代替
button=findViewById(R.id.button);
2.1:
使用:
@ViewsById({R.id.textView,R.id.textView2}) List<TextView> textViewList;代替一个个的去绑定
3:
使用
@Click(R.id.button) public void StarActivity(){ Intent i=new Intent(MainActivity.this,MyService_.class); startService(i); }代替:
button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent i=new Intent(MainActivity.this,MyService_.class); startService(i); } });
3.1:
同样几个button也可以共享一个Onclick事件
@Click({R.id.button,R.id.button1}) public void StarActivity(){ Intent i=new Intent(MainActivity.this,MyService_.class); startService(i); }3.2:
在方法中加参数可以识别不同的view,或者对其做操作
@Click({R.id.button,R.id.button2}) public void StarActivity(View view){ String outString=null; if(view.equals(button)) outString="button"; else if(view.equals(button1)) outString="button1"; else outString="fail"; Toast.makeText(this,outString, Toast.LENGTH_LONG).show(); }3.3:长按,触摸
@Touch(R.id.button)
@LongClick(R.id.button)
3.4:
@ItemClick(R.id.list_view)
也可以使用
4:使用AfterViews
@AfterViews public void setTextView(){ textView.setText("Hello world"); }会在
@ViewById(R.id.textView)
之后对textView进行赋值
5:便捷获取Extra中值
在前一个Activity
Intent i=new Intent(MainActivity.this,SecondActivity_.class); i.putExtra(NAME_KEY,NAME_KEY); i.putExtra(PASSWORD_KEY,PASSWORD_KEY); startActivity(i);在后一个Antivity中可以通过
@Extra(MainActivity.NAME_KEY) String name; @Extra(MainActivity.PASSWORD_KEY) String password;直接取出,并且避免为空验证
6:快速开一个新的线程:
@Background public void doSomeThing(){ Log.d("Annotations",String.valueOf(Thread.currentThread().getId())); }调用
doSomeThing();
就可以新开一个线程做耗时的工作
个人感觉这个功能挺有用的,但是担心会遇到一些问题~~~
6.1
在doSomeThing的子线程中调用主线程中的函数,达到控制主线程view的目的
@Background public void doSomeThing(){ Log.d("Annotations",String.valueOf(Thread.currentThread().getId())); uiThread(); } @UiThread public void uiThread(){ button1.setText("Uithread"); }7:对资源进行获取
在res下string 下定义
<string name="name">annotations</string>然后通过
@StringRes(R.string.name) String name;获取
7.1:获取 dimen的字体大小
<dimen name="text_size">16dp</dimen>然后通过
@DimensionRes(R.dimen.text_size) float textSize;获取
--------------------------------------------------------
在
AndroidManifest.xml
中通过Ctrl+鼠标点击
android:name=".MainActivity_"可以去查看修改后的源代码
更多的标签可以去看API
https://github.com/excilys/androidannotations/wiki/AvailableAnnotations#typesafe-sharedpreferences