Advertisement
AntonioVillanueva

TextWatcher test in kotlin

Dec 13th, 2021
1,132
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 1.62 KB | None | 0 0
  1. package com.example.testtextwatcher
  2.  
  3. import androidx.appcompat.app.AppCompatActivity
  4. import android.os.Bundle
  5. import com.example.testtextwatcher.databinding.ActivityMainBinding
  6. import android.text.TextWatcher
  7. import android.text.Editable
  8.  
  9. class MainActivity : AppCompatActivity() {
  10.     private lateinit var binding: ActivityMainBinding
  11.  
  12.     override fun onCreate(savedInstanceState: Bundle?) {
  13.         super.onCreate(savedInstanceState)
  14.         binding = ActivityMainBinding.inflate(layoutInflater)
  15.  
  16.         val view = binding.root
  17.         setContentView(view)
  18.  
  19.         var entrada =binding.editTextNumberDecimal
  20.         var salida=binding.textView
  21.  
  22.         //texto.setText("algo") //Escritura en el EditText
  23.  
  24.         //TestWatcher A
  25.         //Anado un Textwatcher a la entrada un "EditText"
  26.         entrada.addTextChangedListener(object : TextWatcher {
  27.             var textoLocal=""
  28.  
  29.             override fun afterTextChanged(s: Editable) {}
  30.  
  31.             override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
  32.  
  33.             override fun onTextChanged(s: CharSequence, start: Int,before: Int, count: Int) {
  34.                 //Cuando el texto cambia afecto un textView de salida
  35.                 salida.setText("Texto en el EditText : " + s)
  36.                 textoLocal=s.toString()
  37.                 if (textoLocal.isNotEmpty()){ //Si la cadena no esta vacia
  38.                     if (textoLocal.toInt()>255){ //Si es mayor a 255
  39.  
  40.                         entrada.setText("0")//La seteas a 0
  41.                     }
  42.                 }
  43.             }
  44.         })
  45.         //TestWatcher B
  46.     }
  47. }
  48.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement