良い例
val editText = findViewById<EditText>(R.id.editText) val text = "asdhfasdflasdflkad" editText.setText(text)
おわり。
良くない例
1.型が違う
val editText = findViewById<EditText>(R.id.editText) val text = "asdhfasdflasdflkad" editText.text = text
editText.text は型が Editable! のため良くない
2.文字列連結の場所が良くない
val editText = findViewById<EditText>(R.id.editText) val text = "asdhfasdflasdflkad" editText.setText(text + "1234567890")
setText で文字列を連結させるのは良くないとされるため。(エディタにハイライトされて注意される)
連結後の変数を作っとくとかしたほうがいいかも。
これは Button などの View だと
val Button= findViewById<Button>(R.id.button) val text = "asdhfasdflasdflkad" button.text = text + "1234567890"
みたいな書き方で出てきてしまう。
そもそも Kotlin で setText() を使うのがあんまりかっこよくないのでいや。せっかく Kotlin で書いてるのに Java みたいやん。