16
20

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.

Kotlin Android Extensionsをつかってみる。

Last updated at Posted at 2015-10-28

#まえがき
Android DevelopのトレーニングをKotlinでやってみる -Building Your First App[2]-
http://qiita.com/kamedon39/items/73f956dcc0cf1811b6bc
の番外編です

#Kotlin Android Extensions
https://kotlinlang.org/docs/tutorials/android-plugin.html
Kotlinでレイアウトからviewを簡単にアクセスできる方法があります。
null安全でとっても便利。もちろん補完ききます!

##インストール

  1. android studioにkotlin-android-extensions をインストール
    Settings->Plugins->Install JetBrains plugin ->kotlin-android-extensions(検索などで) ->Download and Install->Apply->Restart

  2. app/build.gradleにkotlin-android-extensionsを記述
    KotlinをAndroid Studioに自動設定してる場合、+の部分を追加するだけ。

buildscript {
    ext.kotlin_version = '1.0.0-beta-1038'
    repositories {
        mavenCentral()
    }
    dependencies {
         classpath "org.jetbrains.kotlin:kotlin-gradle-plugin:$kotlin_version"
+        classpath "org.jetbrains.kotlin:kotlin-android-extensions:$kotlin_version"
    }
}
repositories {
    mavenCentral()
}

##利用法

import kotlinx.android.synthetic.<layout>.*

<layout>はsetContentViewしてるlayout名です
するとid名の変数が作られ、しかもnot null。
すごい便利です。しかもちゃんとキャストまでしてくれる賢いやつです。
レイアウトの修正した時などに、間違って使ってるviewけしちゃってもすぐ気づけます!

例:

import kotlinx.android.synthetic.activity_main.*

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        btn_send.setOnClickListener({ view -> Log.d("MainActivity", "click: btn_send") })
    }
}

#比較
##前提

<Button
    android:id="@+id/btn_send"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"/>

##JAVA

###findViewById

findViewById(R.id.btn_send).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
               Log.d("MainActivity", "click: btn_send") 
            }
});

###xml直記述

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/button_send"
    android:onClick="clickSend" />

###butterknife

    @OnClick(R.id.btn_send)
    void clickSend(View v) {
      Log.d("MainActivity", "click: btn_send") 
    }

##Kotlin
###findViewById

   findViewById(R.id.btn_send).setOnClickListener { view -> Log.d("MainActivity", "click: btn_send") };

###KotterKnife

 val btnSend: Button by bindView(R.id.btn_send)

 btnSend.setOnClickListener({ view -> Log.d("MainActivity", "click: btn_send") })

###Kotlin Android Extensions

import kotlinx.android.synthetic.activity_main.*

btn_send.setOnClickListener({ view -> Log.d("MainActivity", "click: btn_send") })

##github
https://github.com/kamedon/KotlinAndroidTraining

16
20
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
16
20

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?