編輯:Android資訊
本文由碼農網 – 蘇耀東原創,轉載請看清文末的轉載要求,歡迎參與我們的付費投稿計劃!
AndroidAnnotations是一個開源框架,通過使用它開放出來的注解api,可以大大的減少無關痛癢的代碼量,簡潔代碼。
官方文檔(github鏈接)
目前最新版本為4.0.0
在app/目錄下的build.gradle(局部gradle)中添加下面紅色粗體字配置:
applyplugin:'com.android.application'
applyplugin:'android-apt' defAAVersion='4.0.0'
android{
compileSdkVersion23
buildToolsVersion"23.0.2"
defaultConfig{
applicationId"com.xxx.demo"
minSdkVersion18
targetSdkVersion23
versionCode1
versionName"1.0"
}
buildTypes{
release{
minifyEnabledfalse
proguardFilesgetDefaultProguardFile('proguard-android.txt'),'proguard-rules.pro'
}
}
}
dependencies{
compilefileTree(dir:'libs',include:['.jar'])
testCompile'junit:junit:4.12'
compile'com.android.support:appcompat-v7:23.1.1'
*apt"org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion"
}
apt{ arguments{ androidManifestFilevariant.outputs[0].processResources.manifestFile resourcePackageName"com.xxx.demo"(你項目的包名) } }
項目包名可在AndroidManifest.xml中的package確認。
在gradle/目錄下的build.gradle文件(全局gradle)中添加下面紅色粗體字配置:
buildscript{
repositories{
jcenter()
}
dependencies{
// replace with the current version of the Android plugin
classpath'com.android.tools.build1.5.0'
// replace with the current version of the android-apt plugin
classpath'com.neenbedankt.gradle.plugins:android-apt:1.4+'
}
}
allprojects{
repositories{
jcenter()
}
}
taskclean(type:Delete){
deleterootProject.buildDir
}
@EActivity(R.layout.acitvity_main)
public class MainActivity extends Activity{
...
}
常用的有@EActivity、@EFragment、@EService等,進行注解了的組件才可使用其他注解功能。
@ViewById(R.id.tv_title)//此處可去掉括號部分 TextView tv_title; @ViewById ImageView img_menu; @ViewById RelativeLayout rl_light; @Extra String mTitle; @StringRes(R.string.hello) String hello;
簡單的控件綁定,資源文件中的id與控件名一致即可不在注解後加上括號及對應控件的id,@Extra也是。其他地方需要聲明控件id的皆同理。
當View相關的成員變量初始化完畢後,會調用擁有@AfterViews注解的方法,可以在裡面初始化一些界面控件等。
@Click
void img_back() {
finish();
overridePendingTransition(R.anim.zoom_in, R.anim.zoom_out);
}
還有@TextChange、@ItemClick、@SeekBarProgressChange等。
@UiThread
void doSomething(){
...
}
@Background
void doSomething(){
...
}
UI線程執行的方法加個@UiThread,異步線程方法加個@Background,兩者的交互就是方法直接的相互調用,不用再使用Handler去發送接收Message了。
@Receiver(actions = Utils.ACTION_BLE_DISCONNETED)
public void bleDisconnect() {
...
}
@Receiver(actions = Utils.ACTION_UPDATE_WATER_SHOW)
public void updateWaterShow(@Receiver.Extra(Utils.VALUE_ADDRESS) long water) {
if (switchIsOpen)
edt_water.setText(water + "");
}
注冊廣播接收,簡單搞定,不需要其他操作。相比下傳統的方式:
Private final BroadcastReceiver mGattUpdateReceiver = newBroadcastReceiver(){
@Override
public void onReceive(Contextcontext,Intentintent){
final Stringaction=intent.getAction();
if(Stringaction.equal(Utils.ACTION_STOP_SCAN)){
...
}
}
};
private IntentFilter makeGattUpdateIntentFilter() {
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(Utils.ACTION_STOP_SCAN);
return intentFilter;
}
registerReceiver(mGattUpdateReceiver,makeGattUpdateIntentFilter());
unregisterReceiver(mGattUpdateReceiver);
瞬間簡潔了很多吧
直接使用@SharedPref可以簡單地使用SharedPreferences的功能。
首先,建一個類存放需要存取的數據:
@SharedPref(value=SharedPref.Scope.UNIQUE)
public interface MyPrefs {
@DefaultBoolean(true)
boolean isFirstIn();
@DefaultString("")
String ignoreVersion();
@DefaultInt(0)
int shockLevel();
}
括號後面的是默認值,接下來就是簡單的使用了,首先在用到的類裡聲明:
@Pref MyPrefs_ myPrefs; boolean isFirstIn = myPrefs.isFirstIn().get(); myPrefs.isFirstIn().put(false);
使用起來特別方便,需要特別說明的是,這些數據要在一些不同的組件中同步共享,需在@SharedPref加上(value=SharedPref.Scope.UNIQUE),之前在activity和service中的數據總是對不上,找了好久才找到原因。
想要在普通的類中也用上注解,只需在類名加上@EBean
@EBean
public class MyClass {
@UiThread
void updateUI() {
}
使用時,聲明:
@EActivity
public class MyActivity extends Activity {
@Bean
MyClass myClass;
}
有一些要注意的是:
@EBean注解的類,只能有一個構造方法,且這個構造方法必須無參數或者只有context參數。
在activity等組件內聲明了後,不用再去new這個類,否則會出錯。
比較常用的一些方法及說明大概就是這些,當然Annotation還有不少東西,想要了解得更深入可以到文首的鏈接處查看官方的使用說明,進一步了解!
高質量 Android 開發框架 LoonAndroid 詳解
整個框架式不同於androidannotations,Roboguice等ioc框架,這是一個類似spring的實現方式。在整應用的生命周期中找到切入點,然後對a
在 Linux 上將 BQ Aquaris Ubuntu 手機刷成 Android 系統
如果你正好擁有全球第一支運行 Ubuntu 的手機並且希望將 BQ Aquaris E4.5 自帶的 Ubuntu 系統換成 Android,那這篇文章能幫你點小
Android Activity 啟動模式的功能驗證
之前一直都是看別人寫的啟動模式,發現網上大多數的內容都是抄襲來抄襲去,直到最近看了開發藝術這本書,發現之前對啟動模式的理解過於簡單,很多東西都沒有考慮到,為了加深
Android 應用開發性能優化完全分析
1 背景 其實有點不想寫這篇文章的,但是又想寫,有些矛盾。不想寫的原因是隨便上網一搜一堆關於性能的建議,感覺大家你一總結、我一總結的都說到了很多優化注意事項,但是