33
21

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でfindViewByIdとさよならしましょう

Posted at

Kotlin Android Extensions

「Kotlin Android Extensions」というAndroid用の拡張機能について共有します。
Viewの操作がすごく楽になります。

詳しくは下記をご確認ください。
https://kotlinlang.org/docs/tutorials/android-plugin.html

楽になる

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.hoge.hoge.activity.MainActivity">

    <TextView
        android:id="@+id/feed"
        android:layout_width="match_parent"
        android:layout_height="match_parent">
    </TextView>

</android.support.constraint.ConstraintLayout>

例えば上記のようなViewにTextをコードから挿入するとき今まで下記のように書いていたと思います。


TextView text = (TextView)findViewById(R.id.feed);
text.setext("hogehoge");

でも「Kotlin Android Extensions」を使うと


feed.text = "hogehoge"

素晴らしすぎますね。

他にもボタンイベントは


button.setOnClickListener{
}

のようにかけたり
ListViewにアダプターを設定するときは


val adapter = FeedAdapter(this, 0, createExampleData())
feed.adapter = adapter

すばらしい。

33
21
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
33
21

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?