1. 配置
首先去git上下載jar包,一個是androidannotations.jar,一個是androidannotations-api.jar。
新建我們的Android工程,將androidannotations-api.jar放入libs目錄中,然後新建一個目錄compile-lib,將androidannotations放進取,右鍵我們的工程-->Properties-->Java Compiler-->Annotation Processing-->勾選Enable project specific settings和底下的兩個選擇框,然後點擊左側Factory Path,添加我們剛纔compile-lib中的jar包,即可。
2. 框架的使用
注意,使用AndroidAnnotation後,所有四大組件的名字後面都需要加一個"_"。
(1). @EActivity
在Activity的上面使用@符號即可添加註解,例如:
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
<span style="white-space:pre"> </span>@ViewById(R.id.button1)
<span style="white-space:pre"> </span>Button button;
<span style="white-space:pre"> </span>@Override
<span style="white-space:pre"> </span>protected void onCreate(Bundle savedInstanceState) {
<span style="white-space:pre"> </span>super.onCreate(savedInstanceState);
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@Click(R.id.button1)
<span style="white-space:pre"> </span>public void startAcitvity() {
<span style="white-space:pre"> </span>startActivity(new Intent(MainActivity.this, SecondActivity_.class));
<span style="white-space:pre"> </span>}
}
看出來可以很方便的省去我們之前的findViewById方法。
(2). @EService
新建一個Service:
@EService
public class MyService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
Log.i("THR", "onStartCommand");
return super.onStartCommand(intent, flags, startId);
}
@Override
public IBinder onBind(Intent intent) {
return null;
}
}
和Activity的差不多:
@Click(R.id.button1)
public void startService() {
startService(new Intent(this, MyService_.class));
}
注意都需要在manifest文件中配置,不能忘記下劃線"_"。(3). @ViewById查找控件
@ViewById(R.id.textView1)
TextView textView1;
@ViewById(R.id.textView2)
TextView textView2;
@AfterViews
public void setTextView() {
textView1.setText("Text1");
textView2.setText("Text2");
}
如果不指定ViewById後面的id的話,它會默認的去尋找和我們變量名一致的id,如果找到則可以正常運行,找不到就會報空指針的異常。(4). @ViewsById查找多個控件
還有一種方法使用ViewsById:
@ViewsById({ R.id.textView1, R.id.textView2 })
List<TextView> list;
@AfterViews
public void setTextView() {
list.get(0).setText("Text1");
list.get(1).setText("Text2");
}
同樣很簡單。(5). @Click點擊事件
<span style="white-space:pre"> </span>@ViewById(R.id.textView1)
<span style="white-space:pre"> </span>TextView textView1;
<span style="white-space:pre"> </span>@ViewById
<span style="white-space:pre"> </span>TextView textView2;
<span style="white-space:pre"> </span>@Click({ R.id.textView1, R.id.textView2 })
<span style="white-space:pre"> </span>public void showToast() {
<span style="white-space:pre"> </span>Toast.makeText(this, "點擊事件", 1).show();
<span style="white-space:pre"> </span>}
<span style="white-space:pre"> </span>@AfterViews
<span style="white-space:pre"> </span>public void setTextView() {
<span style="white-space:pre"> </span>textView1.setText("TextView1");
<span style="white-space:pre"> </span>textView2.setText("TextView2");
<span style="white-space:pre"> </span>}
使用@Click註解,還可以給多個View添加點擊事件。@LongClick的使用和這個差不多,這裏就不做介紹了。
(6). @Extra可以方便傳遞參數
第一個Activity中:
public static final String NAME = "name";
public static final String AGE = "age";
@Click(R.id.button1)
public void startActivity() {
Intent intent = new Intent(this, SecondActivity_.class);
intent.putExtra(NAME, "Jerry");
intent.putExtra(AGE, 27);
startActivity(intent);
}
SecondActivity中: @Extra(MainActivity.NAME)
String name;
@Extra(MainActivity.AGE)
int age;
@ViewById(R.id.name_view)
TextView nameView;
@ViewById(R.id.age_view)
TextView ageView;
@AfterViews
public void initView() {
nameView.setText(name);
ageView.setText(age + "");
}
(7). @ItemClick、@ItemLongClick和@Touch
這個主要用在ListView上:
@ViewById(R.id.listView)
ListView listView;
@ItemClick(R.id.listView)
public void listViewItemClick(int p) {
Toast.makeText(this, p + "good", 0).show();
}
ItemLongClick和這個使用方法類似。
(8). @Background處理後臺任務的方法
@Background
public void doSomething() {
Log.i("THR", "Background線程id" + Thread.currentThread().getId());
}
@Click(R.id.button1)
public void startActivity() {
doSomething();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Log.i("THR", "MainActivity線程id" + Thread.currentThread().getId());
}
通過運行發現,這個方法是運行在子線程中的。
(9). @UiThread
修改代碼:
@Background
public void doSomething() {
Log.i("THR", "Background線程id" + Thread.currentThread().getId());
updateUi();
}
@UiThread
public void updateUi() {<pre name="code" class="java"><pre name="code" class="java"> Log.i("THR", "updateUi線程id" + Thread.currentThread().getId());
textView1.setText("更新UI");}
可以使得更新UI的操作在主線程中調用,不會報異常。
(10). @StringRes獲取資源文件中的String
@StringRes(R.string.name)
String name;
還有@DimensionRes的使用都類似:
@DimensionRes(R.dimen.activity_horizontal_margin)
double size;
(11). @WindowFeature窗口樣式
無標題樣式:
@WindowFeature({ Window.FEATURE_NO_TITLE, Window.FEATURE_INDETERMINATE_PROGRESS })
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
4. 查看Activity_
右鍵工程-->Java Compiler-->Annotation Processing-->Generated source directory:-->去掉apt_generated前面的點。
5. 總結
在使用這個框架的時候要注意以下幾點:
(1). 註冊同名的Activity_
(2). View的修飾不能爲private的
(3). 註解註釋的方法和字段不能是private的
Log.i("THR", "Background線程id" + Thread.currentThread().getId());