我自己對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)
用法基本和Click相同
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