【Android進階】(1)使用開源架構AndroidAnnotation

來源:互聯網
上載者:User

標籤:android   架構   

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:

@EServicepublic class MyService extends Service {@Overridepublic int onStartCommand(Intent intent, int flags, int startId) {Log.i("THR", "onStartCommand");return super.onStartCommand(intent, flags, startId);}@Overridepublic 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;@AfterViewspublic void setTextView() {textView1.setText("Text1");textView2.setText("Text2");}
如果不指定ViewById後面的id的話,它會預設的去尋找和我們變數名一致的id,如果找到則可以正常運行,找不到就會報null 指標的異常。

(4). @ViewsById尋找多個控制項

還有一種方法使用ViewsById:

@ViewsById({ R.id.textView1, R.id.textView2 })List<TextView> list;@AfterViewspublic 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;@AfterViewspublic 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處理背景工作的方法

@Backgroundpublic void doSomething() {Log.i("THR", "Background線程id" + Thread.currentThread().getId());}@Click(R.id.button1)public void startActivity() {doSomething();}@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);Log.i("THR", "MainActivity線程id" + Thread.currentThread().getId());}
通過運行發現,這個方法是運行在子線程中的。

(9). @UiThread

修改代碼:

@Backgroundpublic void doSomething() {Log.i("THR", "Background線程id" + Thread.currentThread().getId());updateUi();}@UiThreadpublic 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());

著作權聲明:本文為博主原創文章,未經博主允許不得轉載。

【Android進階】(1)使用開源架構AndroidAnnotation

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.