๐Ÿ”ฅTIL๐Ÿ”ฅ์ŠคํŒŒ๋ฅดํƒ€ | addTextChangedListener

hyihyiยท2023๋…„ 12์›” 20์ผ
0

TIL

๋ชฉ๋ก ๋ณด๊ธฐ
23/70
post-thumbnail

-์ฑŒ๋ฆฐ์ง€๋ฐ˜ 1์ฃผ์ฐจ ๊ฐ•์˜

๐Ÿ˜ฎ๊ธฐ์–ตํ•ด์•ผ ํ•  ์  | ์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด์ž

๊ฐ™์€ ํ˜•ํƒœ์˜ ํŒจํ„ด์„ ํ•˜๋‚˜๋กœ ํ•ฉ์น˜๊ณ  ์‹ถ์„ ๋•Œ๋‚˜ ์—ฌ๋Ÿฌ ๊ตฐ๋ฐ์—์„œ ์‚ฌ์šฉํ•˜๋Š” ์ฝ”๋“œ๋ฉด ํ•จ์ˆ˜๋กœ ๋นผ์ž

addTextChangedListener

TextWatcher ์ธํ„ฐํŽ˜์ด์Šค์˜ ๊ฐ„์†Œํ™”๋œ ๋ฒ„์ „

  • ๊ฐ„์†Œํ™”๋œ Kotlin ๋ฐฉ์‹: ๋žŒ๋‹ค๋ฅผ ์‚ฌ์šฉํ•˜์—ฌ ์ฝ”๋“œ๊ฐ€ ๋” ๊ฐ„๊ฒฐํ•˜๊ณ  ์ฝ๊ธฐ ์‰ฝ์Šต๋‹ˆ๋‹ค. ํŠน์ • ๋ฉ”์„œ๋“œ(๋Œ€๋ถ€๋ถ„ afterTextChanged)์— ๋Œ€ํ•œ ๋กœ์ง๋งŒ์„ ๊ตฌํ˜„ํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.
editText.addTextChangedListener {
    // afterTextChanged์— ํ•ด๋‹นํ•˜๋Š” ๋กœ์ง
    editText.setErrorMessage()
}
  • ์ „ํ†ต์ ์ธ ๋ฐฉ์‹: TextWatcher์˜ ๋ชจ๋“  ๋ฉ”์†Œ๋“œ๋ฅผ ๊ตฌํ˜„ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค. ํ…์ŠคํŠธ ๋ณ€๊ฒฝ์˜ ์—ฌ๋Ÿฌ ๋‹จ๊ณ„์— ๋Œ€ํ•ด ๋” ์ƒ์„ธํ•œ ์ œ์–ด๊ฐ€ ๊ฐ€๋Šฅํ•ฉ๋‹ˆ๋‹ค.
editText.addTextChangedListener(object : TextWatcher {
    override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {
        // ํ…์ŠคํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜๊ธฐ ์ „์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
        // s: ๋ณ€๊ฒฝ ์ „์˜ ํ…์ŠคํŠธ
        // start: ๋ณ€๊ฒฝ์ด ์‹œ์ž‘๋˜๋Š” ์œ„์น˜
        // count: ๋ณ€๊ฒฝ๋˜๋Š” ํ…์ŠคํŠธ์˜ ๊ธธ์ด
        // after: ๋ณ€๊ฒฝ ํ›„์˜ ํ…์ŠคํŠธ ๊ธธ์ด
    }

    override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
        // ํ…์ŠคํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋˜๋Š” ๋™์•ˆ์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
        // s: ๋ณ€๊ฒฝ ํ›„์˜ ํ…์ŠคํŠธ
        // start: ๋ณ€๊ฒฝ์ด ์‹œ์ž‘๋˜๋Š” ์œ„์น˜
        // before: ๋ณ€๊ฒฝ๋˜๊ธฐ ์ „์˜ ํ…์ŠคํŠธ ๊ธธ์ด
        // count: ์ƒˆ๋กœ ์ถ”๊ฐ€๋œ ํ…์ŠคํŠธ์˜ ๊ธธ์ด
    }

    override fun afterTextChanged(s: Editable) {
        // ํ…์ŠคํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋œ ํ›„์— ํ˜ธ์ถœ๋ฉ๋‹ˆ๋‹ค.
        // s: ์ตœ์ข… ๋ณ€๊ฒฝ๋œ ํ…์ŠคํŠธ
    }
})

์ฝ”๋“œ์˜ ์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ด๊ธฐ ์œ„ํ•œ ๋ฐฉ๋ฒ•

์ค‘๋ณต๋œ ์ฝ”๋“œ

    private fun initView(){
        
        etName.addTextChangedListener {
            tvNameError.text = getMessageValidName() //๋ทฐ์˜ ๊ฐ€์‹œ์„ฑ์„ ์กฐ์ ˆ์•ˆํ•ด๋„ ๋˜๊ธฐ ๋•Œ๋ฌธ์— ์ด๋ ‡๊ฒŒ ์”€
        }

        etEmail.addTextChangedListener {
            tvEmailError.text = getMessageValidEmail()
        }

        etName.setOnFocusChangeListener{ v, hasFocous ->
            if(hasFocous.not()){
                tvNameError.text = getMessageValidName()
            }
        }

        etEmail.setOnFocusChangeListener{ v, hasFocous ->
            if(hasFocous.not()){
                tvEmailError.text = getMessageValidEmail()
            }
        }
    }

์žฌ์‚ฌ์šฉ์„ฑ์„ ๋†’์ธ ์ฝ”๋“œ

    private val editTexts
        get() = listOf(
            etName,etEmail
        )

    //editTexts๋“ค์„ ์ˆœํšŒํ•˜๋ฉด์„œ addTextChangedListener์™€ setOnFocusChangeListener๋ฅผ ๋“ฑ๋ก
    private fun setEditTextListener(){
        editTexts.forEach { editText ->
            editText.addTextChangedListener{
                when(editText){
                    etName -> {
                        tvNameError.text = getMessageValidName()
                    }
                    etEmail -> {
                        tvEmailError.text = getMessageValidEmail()
                    }
                }
            }

            editText.setOnFocusChangeListener{ v, hasFocous ->
                if(hasFocous.not()){
                    when(editText){
                        etName -> {
                            tvNameError.text = getMessageValidName()
                        }
                        etEmail -> {
                            tvEmailError.text = getMessageValidEmail()
                        }
                    }
                }
            }
        }
    }

๋” ๋†’์ธ ์ฝ”๋“œ..

์—๋Ÿฌ์— ๋Œ€ํ•œ ๋ฉ”์‹œ์ง€ ํŠธ๋ž˜ํ‚น์€ ๋˜‘๊ฐ™์œผ๋‹ˆ ์•„๋ž˜์ฒ˜๋Ÿผ ํ‘œํ˜„ํ•  ์ˆ˜๋„ ์žˆ๋‹ค.

    private fun setEditTextListener(){
        editTexts.forEach { editText ->
            editText.addTextChangedListener{
                editText.setErrorMessage()
            }

            editText.setOnFocusChangeListener{ v, hasFocous ->
                if(hasFocous.not()){
                    editText.setErrorMessage()
                }
            }
        }
    }

    private fun EditText.setErrorMessage(){
        when(this){
            etName -> {
                tvNameError.text = getMessageValidName()
            }
            etEmail -> {
                tvEmailError.text = getMessageValidEmail()
            }
        }
    }

์ฝ”๋“œ ์„ค๋ช…

addTextChangedListener

ํ…์ŠคํŠธ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ๋งˆ๋‹ค ํ˜ธ์ถœ

editText.addTextChangedListener {
    editText.setErrorMessage()
}

setOnFocusChangeListener

ํฌ์ปค์Šค ์ƒํƒœ๊ฐ€ ๋ณ€๊ฒฝ๋  ๋•Œ ํ˜ธ์ถœ
hasFocus๋Š” ๋ถˆ๋ฆฐ ๊ฐ’์œผ๋กœ, EditText๊ฐ€ ํฌ์ปค์Šค๋ฅผ ๊ฐ€์ง€๊ณ  ์žˆ์œผ๋ฉด true, ๊ทธ๋ ‡์ง€ ์•Š์œผ๋ฉด false๋ฅผ ๋ฐ˜ํ™˜
ํฌ์ปค์Šค๋ฅผ ์žƒ์—ˆ์„ ๋•Œ editText.setErrorMessage() ๋ฉ”์„œ๋“œ๊ฐ€ ์‹คํ–‰

editText.setOnFocusChangeListener { v, hasFocus ->
    if (hasFocus.not()) {
        editText.setErrorMessage()
    }
}
profile
๋‚ด๊ฐ€ ์ดํ•ดํ•˜๊ธฐ ์‰ฝ๊ฒŒ ์“ฐ๋Š” ๋ธ”๋กœ๊ทธ

0๊ฐœ์˜ ๋Œ“๊ธ€