3

I have five EditField objects in my BlackBerry app, each one will accept only one numeric character.

I want to change the focus from the first EditField to the second EditField when a character is entered. Note the focus from one to another EditField must go automatically and not by pressing Enter key or some other key.

CRUSADER
  • 5,486
  • 3
  • 28
  • 64
AKASSABOX
  • 75
  • 7

2 Answers2

4

You want to set a FieldChangeListener on the EditField to monitor when the contents of the field changes. Once the user has entered a single character you can move to the next field by calling Field.setFocus().

donturner
  • 17,867
  • 8
  • 59
  • 81
  • Hey having a trouble with this actually I am using Custom EditField but when I change or enter some text in Edit Field the FieldChanged() event isn't called and in turn the focus isn't set to next another Editfield. Is their NE other Event to catch the chnage in text of Editfield other then FieldChangelistner. – AKASSABOX Aug 22 '12 at 09:35
  • `fieldChanged()` is always called when the contents of the `EditField` changes which means that you are doing something in your custom class which is overriding this behaviour. You might want to ask a separate question to get that resolved. – donturner Aug 22 '12 at 12:58
2

Lets assume your EditFields are added to screen one by one.

You could use next code:

editField<i>.setFieldChangeListener(this);
...
public void fieldChanged(Field field, int status) {
   if (field instanceof EditField) {
     EditField editField = (EditField)field;
     if (field.getText().length() > 0) {//don't move focus in case of deleted text
        Manager manager = field.getManager();
        Field nextField = manager.getField(manager.getFieldIndex(editField) + 1);
        if (nextField instanceof EditField) {
           nextField.setFocus();
        }
     }
   }
}
Eugen Martynov
  • 19,888
  • 10
  • 61
  • 114