Home > database >  Change text in realTime
Change text in realTime

Time:10-19

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) {}

CodePudding user response:

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)
        }
  • Related