Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲
 Android教程網 >> Android技術 >> Android資訊 >> AndroidAnnotation常用注解使用說明

AndroidAnnotation常用注解使用說明

編輯: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等。

比較方便的一些注解

異步線程與UI線程

@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);

瞬間簡潔了很多吧

SharedPreferences

直接使用@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中的數據總是對不上,找了好久才找到原因。

@EBen

想要在普通的類中也用上注解,只需在類名加上@EBean

@EBean
public class MyClass {
  @UiThread
  void updateUI() {
}

使用時,聲明:

@EActivity
public class MyActivity extends Activity {
  @Bean
  MyClass myClass;
}

有一些要注意的是:

@EBean注解的類,只能有一個構造方法,且這個構造方法必須無參數或者只有context參數。

在activity等組件內聲明了後,不用再去new這個類,否則會出錯。

總結

比較常用的一些方法及說明大概就是這些,當然Annotation還有不少東西,想要了解得更深入可以到文首的鏈接處查看官方的使用說明,進一步了解!

  1. 上一頁:
  2. 下一頁:
熱門文章
閱讀排行版
Copyright © Android教程網 All Rights Reserved