0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

AndroidStudioでのandroidannotations導入

Last updated at Posted at 2017-10-26

急にAndroidアプリを作ることになってしまった人が、知識ゼロからひとまず自力で導入できるかどうかやってみる。

2017/10/26現在での条件

  • AndroidStudio 2.3.3
  • androidannotations 4.3.1

何はともあれ公式ドキュメント
https://github.com/androidannotations/androidannotations/wiki/Building-Project-Gradle

手順

##1.GradleのConfiguration

build.gradle(Module: app)のdependenciesのところにannotationProcessorとcompileを書き足す

def AAVersion = '4.3.1'
dependencies {
    // 中略
    annotationProcessor "org.androidannotations:androidannotations:$AAVersion"
    compile "org.androidannotations:androidannotations-api:$AAVersion"
}

##2.ビルドしてみる

10:26 Executing tasks: [clean, :app:generateDebugSources, :app:compileDebugSources]
10:26 Gradle build finished in 17s 690ms
10:26 Gradle sync started
10:26 Gradle sync completed

おお、なんだか成功したようだ

#使ってみる
ためしにFragmentに配置したボタンにイベントハンドラをつける。

AndroidManifest.xml
<application>
    <activity android:name=".MainActivity_"> // _をつける
MainActivity.java
@EActivity(R.layout.activity_main) // 追加
public class MainActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // 中略
        // Fragmentを追加するところを修正
        MainFragment fragment = new MainFragment_(); // _をつける
        getFragmentManager().beginTransaction().add(R.id.container, fragment).commit();
    }
}
fragment_main.xml
    <Button
        android:id="@+id/SearchButton" />
...以下略
MainFragment.java
@EFragment(R.layout.fragment_main)
public class MainFragment extends Fragment {
    @Click
    // fragment_main.xmlで指定したidをそのままメソッド名にする
    public void SearchButton(View view) {
        Log.d("MainFragment ", "ボタンが押されました");
    }
}

ボタンを押すとlogcatにログが出る。

以上。

0
3
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
3

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?