0
votes

Problem

I would like to handle the back button on textfield using titanium.

I know there is an event android:back on window but it does not fire on textfield.

How can I handle the back button (or the keyboard hide event) on a textfield using titanium ?

EDIT: here is some code and clarification to illustrate what I want to say:

Step to reproduce :

  1. click the textfield > focus event is fired, the keyboard is shown
  2. hit back button > the keyboard is hidden but the blur event is not called and the textfield hasn't lost the focus

Code:

var textfield = Ti.UI.createTextfield();
textfield.addEventListener('android:back', function() {
  // this method is never called, so this event does not run on textfield
});

textfield.addEventListener('focus', function() {
  // this method is called at step 1
});

textfield.addEventListener('blur', function() {
  // this method is not called at step 2 because 
  // the back button only hide the keyboard but the focus is not lost
});

// what code should I use to catch event when the keyboard is hidden 
// when pressing the back button ?
1

1 Answers

0
votes

The blur event is fired whenever the keyboard is closed. Here is the doc for it.

You can listen to it pretty easily, assuming you created a textField object of type Titanium.UI.TextField:

textField.addEventListener('blur', function(e) {
    var val = e.value; // Get current value when the keyboard closed (the right way) as opposed to textField.value (the wrong way)
    alert("The text is : "+value);
});

If you want to listen for the android:back event you have to listen on the window.

var win = Ti.UI.currentWindow; 
win.addEventListener('android:back', function(e) {
    // do crazy things
});

According to the docs this should work, if not possibly a bug with Titanium?