Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.example.testtextwatcher
- import androidx.appcompat.app.AppCompatActivity
- import android.os.Bundle
- import com.example.testtextwatcher.databinding.ActivityMainBinding
- import android.text.TextWatcher
- import android.text.Editable
- class MainActivity : AppCompatActivity() {
- private lateinit var binding: ActivityMainBinding
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- binding = ActivityMainBinding.inflate(layoutInflater)
- val view = binding.root
- setContentView(view)
- var entrada =binding.editTextNumberDecimal
- var salida=binding.textView
- //texto.setText("algo") //Escritura en el EditText
- //TestWatcher A
- //Anado un Textwatcher a la entrada un "EditText"
- entrada.addTextChangedListener(object : TextWatcher {
- var textoLocal=""
- override fun afterTextChanged(s: Editable) {}
- override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
- override fun onTextChanged(s: CharSequence, start: Int,before: Int, count: Int) {
- //Cuando el texto cambia afecto un textView de salida
- salida.setText("Texto en el EditText : " + s)
- textoLocal=s.toString()
- if (textoLocal.isNotEmpty()){ //Si la cadena no esta vacia
- if (textoLocal.toInt()>255){ //Si es mayor a 255
- entrada.setText("0")//La seteas a 0
- }
- }
- }
- })
- //TestWatcher B
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement