0
0

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 はじめました (1) プロジェクト作成〜TextViewを変更

Last updated at Posted at 2018-04-20

自分はJavaでAndroid開発するのもちょっと自信ない感じなので、きままに勉強のために書いていこうと思います。もし間違ってるところなどあればご指摘いただけると嬉しいですmm

環境

android studio 3.0.1
kotlin

まずはプロジェクト作成

kotというプロジェクトを作って include Kotlin supportにチェックをいれて、Empty Activityを作った。

まずは起動してみる

あたりまえだけど、

actity_main.xml
<TextView
  android:text="Hello World!"
...

とかいてあるので、Hello World!と書かれた画面が出てきた。

0.1f6y1d3wtjd.png

Hello Worldのテキストを動的に変えてみる

まずはIDを追加。main_text_viewというidにしてみた。

actity_main.xml
<TextView
   android:id="@+id/main_text_view"
        ...

Kotlin Android Extensions という便利機能が標準ではいってるらしいので、Javaの時のように findViewById を使わなくてもアクセスできる模様。

MainActivity.kt
import kotlinx.android.synthetic.main.activity_main.* // 追加

class MainActivity : AppCompatActivity() {

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        main_text_view.text = "hoge" // 追加
    }

これで再度実行すると、ちゃんと表示される文字が hoge に変わった。便利!

0.0tzt2w2yun0j.png

0
0
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
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?