개발 창고/Android

[Kotlin] TextView와 EditText에 텍스트를 입력하는 방법

로이제로 2023. 7. 25. 22:00
반응형

0. 미리보기

// TextView
findViewById<TextView>(R.id.tvTitle).text = "안녕하세요"

// EditText
findViewById<EditText>(R.id.etTitle).setText("안녕하세요")

 

1. TextView

TextView에서는 조회한 뷰 클래스에 text 값을 입력해주면 됩니다.

findViewById<TextView>(R.id.tvTitle).text = "안녕하세요"

 

2. EditText

EditText의 경우 TextView 처럼 텍스트를 입력하면 아래와 같은 오류가 발생합니다.

EditText의 잘못된 사용방법

EditText에서 text는 Editable이 입력되어야 하기 때문인데, 이 경우 당황하지 말고 그냥 setText를 써주면 해결됩니다.

findViewById<EditText>(R.id.etTitle).setText("안녕하세요")

 

※ 이 글은 워드프레스에 작성한 글과 동일한 작성자의 동일한 글입니다.

https://royfactory.net/2023/07/02/kotlin-enter-text-textview-edittext/

 

[Kotlin] To enter text in TextView and EditText - ROY FACTORY

The way you type text in TextView and EditText in Kotlin on Android is subtly different. Here you will learn a little bit about how to do it.

royfactory.net

반응형