본문 바로가기
개발/Android

Android EditText, TextView 코틀린 변환 시 JvmOverloads 쓰지 마세요

by Dev Aaron 2020. 12. 16.
반응형

커스터마이징한 EditText가 필요해서 아래와 같이 코드를 작성하였는데
컴파일 에러나 런타임 에러는 없는데, 이상하게 원하는데로 동작하지가 않았다.

class MyEditText @JvmOverloads constructor(
        context: Context?,
        attrs: AttributeSet? = null,
        defStyle: Int = 0
) : AppCompatEditText(context!!, attrs, defStyle) 

알고 보니 생성자를 아래와 같이 선언해야 한다.

class MyEditText : AppCompatEditText(context!!, attrs, defStyle) {
    constructor(context: Context?) : super(context!!) {}
    constructor(context: Context?, attrs: AttributeSet?) : super(context!!, attrs) {}
    constructor(context: Context?, attrs: AttributeSet?, defStyle: Int) : super(context!!, attrs, defStyle) {}
}

이유는 TextView와 EditText의 생성자에서 defStyle을 자체 기본 값으로 덮어쓰기 때문이다.

반응형