-1

I've activity has an editText, I wanna check entered text on the run, if text equals "1" then things r gonna change .. how to make this in Kotlin??

fun EditText.afterTextChanged(afterTextChanged: (String) -> Unit) {
    this.addTextChangedListener(object : TextWatcher {
        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            textView9.visibility = View.GONE
            spin7.visibility = View.GONE
            spin8.visibility = View.GONE
            spin9.visibility = View.GONE
        }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            val Edit = editText.text.toString()
            if (Edit.equals("1")){
                textView9.visibility = View.GONE
                spin7.visibility = View.GONE
                spin8.visibility = View.GONE
                spin9.visibility = View.GONE
            }
            else {
                textView9.visibility = View.VISIBLE
                spin7.visibility = View.VISIBLE
                spin8.visibility = View.VISIBLE
                spin9.visibility = View.VISIBLE
            }
        }

        override fun afterTextChanged(editable: Editable?) {
        }
    })
}
Vadim Kotov
  • 8,084
  • 8
  • 48
  • 62

1 Answers1

0

Try this:

yourEditText.addTextChangedListener(object : TextWatcher {
        override fun afterTextChanged(p0: Editable?) {
            }

        override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

        override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            val text = p0.toString()
            if (text == "1") {
                //do something
            } else {
                //do something else
            }
         }
     })

You can visualize how the methods work here (source):

enter image description here

grrigore
  • 1,050
  • 1
  • 21
  • 39
  • I knew wat was wrong with my answer, U must define that fun inside Activity OnCreate, I was defining them out of the onCreate event, so it didn't work for me .. But now, it works fine. – Mohammed Helal Nov 14 '19 at 16:40