0

As described in ScriptUI reference, there are 2 ways to implement changing listener on EditText:

editText.onChanging = function() {
    ...
}

editText.addEventListener('changing', function(event) {
    ...
})

However, the function is triggered after an edit to EditText.text has been made, denying opportunity to obtain old text value as a fallback option is new value is undesired. I'm looking for something like this:

editText.onChanging = function(oldValue) {
    var newValue = this.text
    if (!isCorrect(newValue)) {
        this.text = oldValue
    }
}
Hendra Anggrian
  • 5,780
  • 13
  • 57
  • 97
  • 1
    Can you grab the text when the editText gets focus? There's an onActivate callback for After Effects, don't know if it works for all aps: https://estk.aenhancers.com/user-interface-tools/control-objects.html#onactivate – stib Aug 25 '20 at 14:13
  • This is a clever workaround, I guess I should have been more creative. Post it as an answer so I can close this post. – Hendra Anggrian Aug 25 '20 at 17:57

1 Answers1

0

editText boxes have an onActivate callback which is triggered when the element gets focus. If you store the current text value of the editText at that point, it can be returned to its original state if the new value doesn't validate.

Note that it might be worth thinking about the UX here—if you enter an invalid value, and the value just reverts, you might not realise that it has done so. For the user this could be very confusing: it looks like I enter a value, but nothing happens. It might be better to throw an error, such as a dialog, or disable other controls, e.g. grey out the "do the things" button if the parameters are incorrect.

stib
  • 3,346
  • 2
  • 30
  • 38