まず、AndroidAnnotationsって何かというと、
Androidのコーディングをしていると誰でも思うであろう、
「またこのコード書くのか・・・。」とか「内部クラスうぜぇ」とか
「書くこと多いな」とかをアノテーションで解決していこうっていうライブラリです。
AndroiAnnotations
まずはここからzipをDL!
https://github.com/excilys/androidannotations/wiki/Download
解凍すると、以下のファイルがあると思います。
- androidannotations-xxx-api.jar
- androidannotations-xxx.jar
- 「xxx」はバージョン番号です
- androidannotations-xxx-api.jarをAndroidProjectのlibsディレクトリに配置。(libsディレクトリがない場合は自分で作成)
- 1で追加したandroidannotations-xxx-api.jarをビルドパスに追加。
- androidannotations-xxx.jarをAndroidProjectのext-libsディレクトリに配置。(libsディレクトリで無ければ別名でもOK)
- AndroidProjectを右クリックして「Properties」→「Java Compiler」→「Annotation Processing」と選択して、「Enable project specific settings」にチェックを入れる。また、「Enable annotation processing」にもチェックを付ける。
- AndroidProjectを右クリックして「Properties」→「Java Compiler」→「Annotation Processing」→「Factory Path」と移動して、3で追加したandroidannotations-xxx.jarを追加する。
せっかくなのでサンプルを一つ。
まずは、AndroidAnnotationsを使用しないパターン。
使用するレイアウトは以下の「my_activity.xml」。
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- >
- <EditText
- android:id="@+id/myInput"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- /gt
- <Button
- android:id="@+id/myButton"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- android:text="Click me!"
- />
- <TextView
- android:id="@+id/myTextView"
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- </LinearLayout>
- public class MyActivity extends Activity {
- protected EditText myInput;
- protected TextView myTextView;
- @Override
- public void onCreate(final Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.my_activity);
- myInput = (EditText)findViewById(R.id.myInput);
- myTextView = (TextView)findViewById(R.id.myTextView);
- final Button myButton = (Button)findViewById(R.id.myButton);
- myButton.setOnClickListener(new OnClickListener() {
- @Override
- public void onClick(final View v) {
- final String name = myInput.getText().toString();
- myTextView.setText("Hello " + name);
- }
- );
- }
- }
加工して別のエリアに表示するだけのプログラムです。
これを、AndroidAnnotationsを使用して書くと、以下のようになります。
使用するレイアウトを同じものでOKです。
- @EActivity(R.layout.my_activity)
- public class MyActivity extends Activity {
- @ViewById(R.id.myInput)
- protected EditText myInput;
- @ViewById(R.id.myTextView)
- protected TextView myTextView;
- @Click
- protected void myButton() {
- final String name = myInput.getText().toString();
- myTextView.setText("Hello " + name);
- }
- }
これだけでOKです。
各アノテーションの説明を。
- @EActivity
- Activityクラスの付けるアノテーションです。Activityで使用するレイアウトxmlを指定します。
- @ViewById
- フィールドに付けるアノテーションです。指定したidのviewの参照をフィールド変数にセットします。
- @Click
- メソッドに付けるアノテーションです。メソッド名とレイアウト内のidを同一にする必要があります。このアノテーションをつけたメソッドは対象のidのviewがクリックされた時の動作になります。
AndroidManifest.xmlに記述する場合は、acticity名を以下のようにする必要があります。
ActivityがMyActivityの場合、android:name=".MyActivity_"とします。
"_"を付けるってことです。
次回から、いろいろ紹介していこうと思います。
0 件のコメント:
コメントを投稿