Android應用開發架構:Android Annotations Android Annotations是一個開源的架構,用於加速 Android應 用的開發,可以讓你把重點放在功能的實現上,簡化了代碼,提升了可維護性。其實編程的開源的世界裡有很多很多書本上沒有但是github全球最大的程式員 社交網站和代碼託管網站裡有很多很多有用的項目和開原始碼,比如這個Android應用開發架構Android Annotations,所以英語水平的提高勢在必行啊,否則我永遠只能等到翻譯組翻譯出來猜輪到我啃一啃新的知識,技術和應用,這樣就晚了別人很多很多 步了。 特性: •依賴注入: inject views, extras, system services, resources, ... •簡化的執行緒模式: annotate your methods so that they execute on the UI thread or on a background thread. •Event 綁定: annotate methods to handle events on views, no more ugly anonymous listener classes! •REST 用戶端: create a client interface, AndroidAnnotations generates the implementation. •AndroidAnnotations provide those good things and even more for less than 50kb, without any runtime perf impact! 程式碼範例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
@EActivity(R.layout.translate) // Sets content view to R.layout.translate public class TranslateActivity extends Activity { @ViewById // Injects R.id.textInput EditText textInput; @ViewById(R.id.myTextView) // Injects R.id.myTextView TextView result; @AnimationRes // Injects android.R.anim.fade_in Animation fadeIn; @Click // When R.id.doTranslate button is clicked void doTranslate() { translateInBackground(textInput.getText().toString()); } @Background // Executed in a background thread void translateInBackground(String textToTranslate) { String translatedText = callGoogleTranslate(textToTranslate); showResult(translatedText); } @UiThread // Executed in the ui thread void showResult(String translatedText) { result.setText(translatedText); result.startAnimation(fadeIn); } // [...] } |
項目首頁:http://www.open-open.com/lib/view/home/1367820981261 這個項目首頁裡可以找到下載Android Annotations的jar包。 然後下面就是樣本怎麼使用這Android Annotations的jar包: 備忘:一個api的jar 包放在原來已經有的libs檔案夾下面,然後另一個jar 包放在自己建立一個compilb-lib檔案夾下面。 備忘:在Java Complier中找到Anootation Processing. 如果沒有很有可能的原因你使用的是安卓官方網站提供的專門用來開發安卓的配置好安卓sdk的adt-eclipse,所以就需要下載相關的配置。 然 後選擇juno安裝,接著因為檔案太多不易找到就在type filter text中輸入要下載的Eclipse Java Development Tools,然後可能有不同版本,自然選最新的版本,或者下面勾選顯示最新的版本,就會有唯一的這個Tools出來了,然後next繼續點擊知道連網下載 和安裝,最後提示重新啟動 重新啟動後,就能在Java Complier中找到Anootation Processing。 然後我們接著切入主題: 選中Annotation Processing之後,然後勾選選中Enabel project specific settings。然後選中Annotation Processing下的Factory Path: 也是勾選選中Enabel project specific settings,然後點擊Add JARs,將檔案夾下的compile-lib下的jar 包添加進去,然後應用,然後ok。這樣就能使用這個Android Annotations開源jar包了。 下面就簡單的說明怎麼使用以及使用的效果,你可以和你原來正規情況下的安卓應用程式開發相對比,你就知道這個Android Annotations開源項目帶來的好處是多麼大了。 使用過程樣本: 1、首先在Manifest資訊清單檔中修改一下,就是在android:name=".MainActivity"後面加一個“_”變成android:name=".MainActivity_",先不要問為什麼,這個知識點當然是Android Annotations開源項目的相關使用知識了。 2、然後回到MainActivity.java中,實行以下的操作: 3、然後通過自己手機運行可是成功。
|