
این کتابخانه بسیاری از کارهای خسته کننده را برای ما جذاب کرده است به تکه کد زیر دقت کنید
@Click({R.id.updateBookmarksButton1, R.id.updateBookmarksButton2}) void updateBookmarksClicked() { searchAsync(search.getText().toString(), application.getUserId()); }
این تکه کد جای دو عددبلاک کد set on click را می گیرد.و مزیت دیگری که داره اینه که هر کسی بعد از شما تنها با دید این تکه کد می فهمد جریان چیه…
پس چرا از این کتابخانه استفاده نکنیم.. برای استفاده از کتابخانه دوتا نکته لازم ه که رعایت کنید
۱- وابستگی هایش را اضافه کنید
buildscript { repositories { mavenCentral() } dependencies { // replace with the current version of the Android plugin classpath 'com.android.tools.build:gradle:2.3.0' } } repositories { mavenCentral() mavenLocal() } apply plugin: 'com.android.application' def AAVersion = 'XXX' dependencies { annotationProcessor "org.androidannotations:androidannotations:$AAVersion" compile "org.androidannotations:androidannotations-api:$AAVersion" } android { compileSdkVersion 25 buildToolsVersion "25.0.2" defaultConfig { minSdkVersion 9 targetSdkVersion 25 // If you have different applicationIds for buildTypes or productFlavors uncomment this block. //javaCompileOptions { // annotationProcessorOptions { // arguments = ["resourcePackageName": android.defaultConfig.applicationId] // } //} } }
۲- در هر اکتیوتی که می خواهید ازش استفاده کنید باید در منیفست اسم اکتیوتی را به این شکل عوض کنید
<activity android:name=".Retrofit.ApiActivity"/>;
را به این تغییر بدهیم به _ آخرش دقت کنید
<activity android:name=".Retrofit.ApiActivity_"/>;
خب بعد از اینها میریم سراغ موارد حفظی..
۱-اولین مورد
1@EActivity(R.layout.translate) // Sets content view to R.layout.translate
این مورد به راحتی لایوت مورد نظر را به اکتیوتی متصل می کند.
۲-وصل کردن ویو ها به اکتیوتی
@ViewById // Injects R.id.textInputEditText textInput; @ViewById(R.id.myTextView) // Injects R.id.myTextViewTextView result;
اگر به مورد اول دقت کنید یک ساده سازی دیگر نیز کرده است اینکه id ویو را با نامش یکی گذاشته و نیازی نیست دوبار R.id.. فلان را بنویسد
۳-استفاده از ریسورس ها مثل انیمیشن
می تونید از ریسورس ها مثل استرینگ ها عکس ها و انیمیشن ها هم با یک خط کد استفاده کنید
@AnimationRes // Injects android.R.anim.fade_inAnimation fadeIn;
۴-به طور مثال می توانید کلیک رو هر چیز را با یک خط کد مدیریت و ایجاد کنید
@Click // When R.id.doTranslate button is clicked void doTranslate() { translateInBackground(textInput.getText().toString()); }
۵-اخرین موردی که بررسی می کنیم اجرا بخشی از کد در Thread اصلی و یا در background است
@Background // Executed in a background threadvoid translateInBackground(String textToTranslate) { String translatedText=callGoogleTranslate(textToTranslate); showResult(translatedText); } @UiThread // Executed in the ui threadvoid showResult(String translatedText) {result.setText(translatedText);result.startAnimation(fadeIn);}}
در اخر هم کل کد یک اکتیویتی رو قرار می دم که بدونید کجا ها قرار میدیم این annotations ها رو
@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); } }