Change text in realTime

67 Views Asked by At

Need to replace text inside ediTtext so that the letters are replaced realTime + . Store values in map Of("A" to "B") + desired literal; example: writes A output B or a output b

binding.etPassword.addTextChangedListener(object : TextWatcher {
            override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {

            }

            override fun afterTextChanged(s: Editable) {
                binding.etPassword.removeTextChangedListener(this)
                val mapping = mapOf("A" to "B")
                val text = s.toString().replace("A", "B")
                s.replace(0, s.length, text)
                binding.etPassword.addTextChangedListener(this)
            }

            override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {}
1

There are 1 best solutions below

0
Tenfour04 On BEST ANSWER

You need to iterate the mapping, changing the previous output each time.

        override fun afterTextChanged(s: Editable) {
            binding.etPassword.removeTextChangedListener(this)
            val mapping = mapOf("A" to "B")
            var text = s.toString()
            for ((a, b) in mapping) {
                text = text.replace(a, b)
            }
            s.replace(0, s.length, text)
            binding.etPassword.addTextChangedListener(this)
        }