Android注釈フレームワークAndroid Annotations


まずAndroidAnnotationsは比較的包括的な注釈フレームワークである.
次は公式解釈を見てみましょう:Fast Android Development.Easy maintenance. AndroidAnnotations is an Open Source framework that speeds up Android development. It takes care of the plumbing, and lets you concentrate on what’s really important. By simplifying your code, it facilitates its maintenance.
急速なAndroid開発で、メンテナンスが容易です.Android Annotationsは、Android開発を迅速に行えるオープンソースフレームワークです.コードをより簡素化し、本当に重要な場所に集中させます.コードを簡素化することで、プロジェクトのメンテナンスが容易になります.
1.Android Annotationsのダウンロード
Githubダウンロード:https://github.com/excilys/androidannotations
2.Android Annotationsの導入
開発環境でAndroid Studioを使用
(1)build.gradleファイルに依存を追加します.次のようにします.
apply plugin: 'com.android.application'
// AndroidAnnotations
apply plugin: 'com.neenbedankt.android-apt'
def AAVersion = '4.0.0'

android {
    ...
}

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])

    // AndroidAnnotations
    apt "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

// apt settings
apt {
    arguments {
        androidManifestFile variant.outputs[0].processResources.manifestFile
    }
}

3.注入Avtivity
Activityを見てみましょう
public class MainActivity extends Activity {
    TextView textView;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        textView = (TextView) findViewById(R.id.textView);
        //    View
        initView();
    }
    public void initView() {
       ...
    }
}

注入後:
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
    @ViewById(R.id.textView)
    TextView textView;
    @AfterViews
    void initView() {
        ...
    }
}

注意:AndroidManifestをxmlでMainActivityをMainActivity_に変更し、これはAndroid Annotationsの特別な場所で、変更が終わったらbuildが工事をしたことを覚えています.
<activity android:name=".MainActivity" /> 
  :
<activity android:name=".MainActivity_" />

4.注入クリックイベント
Activityを見てみましょう
public class MainActivity extends Activity implements View.OnClickListener {
    Button button;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        button = (Button) findViewById(R.id.button);
        button.setOnClickListener(this);
    }
    @Override
    public void onClick(View v) {
        switch(v.getId()) {
            case R.id.button:
                Intent intent = new Intent(this, TwoActivity.class);
                intent.putExtra("message", "haha!");
                startActivity(intent);
                break;
        }
    }
}

注入後implements View.OnClickListenerこれらは書く必要はありません:
@EActivity(R.layout.activity_main)
public class MainActivity extends Activity {
    @ViewById(R.id.button)
    Button button;

    @Click(R.id.button)
    void buttonAction(View view) {
        TwoActivity_.intent(this).msg("haha!").start();
    }
}

6.Android Annotationsのソースコードを深く勉強する
公式のOverview:Android Annotations works in a very simple wayを見てみましょう.It automatically adds an extra compilation step that generates source code, using the standard Java Annotation Processing Tool. What source code ? For each enhanced class, for example each @EActivity annotated activity, a subclass of this activity is generated, with the same name plus an underscore appended at the end. For instance, the following class:
package com.some.company;
@EActivity
public class MyActivity extends Activity {
  // ...
}

Will generate the following subclass, in the same package but in another source folder:
package com.some.company;
public final class MyActivity_ extends MyActivity {
  // ...
}

This subclass adds behavior to your activity by overriding some methods (for instance onCreate()), yet delegating the calls to super.
That is the reason why you must add _ to your activity names in AndroidManifest.xml:
<activity android:name=".MyListActivity_" />

Android Annotationsは非常に簡単な仕事の方法です.JavaのAPT(標準java注釈処理ツール、jdk 1.6が導入した特性)を自動的に使用して、Javaソースコードを生成する追加のコンパイルステップを追加します.ソースコードは何ですか?各エンハンスクラスでは、@EActivityコメントのActivityごとにサブクラスが生成されます.このサブクラスは下線で、独自のActivityから継承されます.たとえば、次のクラスがあります.
package com.some.company;
@EActivity
public class MyActivity extends Activity {
  // ...
}

次のサブクラスが生成されます.同じパッケージにありますが、別のソースフォルダにあります.
package com.some.company;
public final class MyActivity_ extends MyActivity {
  // ...
}

このクラスは、oncreate()などのいくつかの方法を書き換えることで動作アクティビティを追加しますが、親クラスの呼び出しを依頼します.
それがAndroidManifestでなければならない理由ですxmlにXxxxActivity_を追加理由:
<activity android:name=".MyListActivity_" />