Skip to content

Instantly share code, notes, and snippets.

@WrathChaos
Last active March 2, 2020 17:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save WrathChaos/bdaff194194908a29c0f04dda9f1cc46 to your computer and use it in GitHub Desktop.
Save WrathChaos/bdaff194194908a29c0f04dda9f1cc46 to your computer and use it in GitHub Desktop.
How to use TextWatcher for more than one EditText?
val textWatcher = object : TextWatcher {
    override
    fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2:Int) {

    } 

    override
    fun onTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {

    }

    override
    fun afterTextChanged(editable: Editable?) {
        if (editable != null && !editable.toString().equals("")) {
            // Checking editable.hashCode() to understand which edittext is using right now
            if (editText.editText!!.text.hashCode() === editable.hashCode()) {
                // This is just an example, your magic will be here!
                val value = editable.toString()
                editText.editText!!.removeTextChangedListener(this)
                editText.editText!!.setText(value)
                editText.editText!!.addTextChangedListener(this)
            }
        } else if (editText2.editText!!.text.hashCode() === editable!!.hashCode()) {
            // This is just an example, your magic will be here!
            val value = editable!!.toString()
            editText2.editText!!.removeTextChangedListener(this)
            editText2.editText!!.setText(value)
            editText2.editText!!.addTextChangedListener(this)
        }
    }
}
@yamilic008
Copy link

Disculpa cuando trato de hacer la implementacion, no me permite utilizar el editText.editText !!

mi codigo lo tengo de la siguiente manera txt_importe.editText!!.text.hashCode() === s.hashCode()

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment