Get old and new value from EditText


#1

Hello, how can I get the old and the new value of an EditText (for a calculation) ?

First, the EditText is empty. Then, the user can change this value and see a result.
But if he change again this value, how can I detect if it’s the same that the other ?

this is my function :

private fun calc(quantite: Int, prix: Float, total: Float): Float{
    val result = quantite * prix
    val newTotal = result + total
    val totalView =  findViewById<TextView>(R.id.textViewTotal)
    totalView.text = newTotal.toString()
    return newTotal
}

and my listener :

if(textViewLabel.text == "Quantité"){
    input.onFocusChangeListener = View.OnFocusChangeListener { view, hasFocus ->
        if (!hasFocus && input.text.toString() != "") {
            val quantite = Integer.parseInt(input.text.toString())
            Log.i(TAG, quantite.toString())
            total = calc(quantite, 1.0f, total)
            Toast.makeText(baseContext, "focus off", Toast.LENGTH_SHORT).show()
        }
    }
}

I need to check is the old value is > or < than the new, so that I can change my function to add / remove some value (I didn’t wrote this part yet)


#2

I’ve used TextWatcher and afterTextChanged to achieve this


#3

you can call EditText’s addTextChangedListener(),and pass an instance of TextWatcher