App노자

[Android] TextWatcher 본문

Android/AndroidStudio

[Android] TextWatcher

앱의노예 2023. 5. 25. 09:01

1. TextWatcher란


Android에서 유저가 뭔가를 입력할 때마다 실시간으로 관찰하면서 입력값에 대해 이벤트를 처리해야 하는 경우가 있다

이럴 때 사용하는 게 addTextChangedListener와 TextWatcher이다

addTextChangedListener는 EditText에 추가적인 글자 변화가 있는지 관찰을 하고 있는 리스너이고

TextWatcher는 값의 입력시점에 (전, 중, 후) 관한 메서드를 갖고 있는 인터페이스이다

 

https://developer.android.com/reference/android/text/TextWatcher

 

TextWatcher  |  Android Developers

 

developer.android.com

2. 사용방법


class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var mEditText: EditText = findViewById(R.id.mEditText)
        
        mEditText.addTextChangedListener(object : TextWatcher{
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                TODO("Not yet implemented")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                TODO("Not yet implemented")
            }

            override fun afterTextChanged(p0: Editable?) {
                TODO("Not yet implemented")
            }
        })
    }
}
beforeTextChanged (변경 전)

새로운 텍스트로 대체될 예정임을 알리기 위해 호출된다 이 콜백에서는 p0을 변경하면 안 된다

 

onTextChanged (변경 중)

이전 텍스트를 대체했음을 알리기 위해 호출된다 이 콜백에서는 p0을 변경하면 안 된다

 

afterTextChanged (변경 후)

Text가 변경되었음을 알리기 위해 호출된다

이 콜백에서는 p0을 수정하는 것이 가능하나 무한 루프에 빠지지 않도록 주의해서 변경하여야 한다

 

class MainActivity : AppCompatActivity() {
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        var mEditText1: EditText = findViewById(R.id.mEditText)
        var mEditText2: EditText = findViewById(R.id.mEditText2)

        val mTextWatcher: TextWatcher = object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                TODO("Not yet implemented")
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
                TODO("Not yet implemented")
            }

            override fun afterTextChanged(p0: Editable?) {
                TODO("Not yet implemented")
            }

        }

        mEditText1.addTextChangedListener(mTextWatcher)
        mEditText2.addTextChangedListener(mTextWatcher)

    }
}

같은 내용을 여러 번 사용하는 경우에는 위와 같은 방법으로 선언을 따로 해서 사용하는 방법도 있다

아래는 stackoverflow에서 각각의 차이점을 자세하게 설명한 내용이다

https://stackoverflow.com/questions/20278382/differences-between-textwatcher-s-ontextchanged-beforetextchanged-and-aftertex/20278548

 

Differences between TextWatcher 's onTextChanged, beforeTextChanged and afterTextChanged

In my Android project, I have had to add a TextChangedListener (TextWatcher) to an edit text view. And there are three parts to it: onTextChanged() beforeTextChanged() afterTextChanged() What a...

stackoverflow.com

 

'Android > AndroidStudio' 카테고리의 다른 글

[Android] onNewIntent()  (0) 2023.06.01
[Android] Log확인 - getSimpleName()  (0) 2023.05.31
[Android] ViewTreeObserver란  (0) 2023.05.17
[Android] customSelectionActionMode 구현 방법  (0) 2023.04.29
[Android] DialogFragment란  (0) 2023.04.22