Search code examples
searchblackberryfocusblackberry-editfield

Callback while typing a character in editfield blackberry


I need to get a callback with every character typed or deleted in EditField in BlackBerry. I need to get the text of EditField as soon as it is written, without losing focus.


Solution

  • There's multiple ways to do this. For example, if you have an EditField instance like this:

    private EditField _editField;
    

    then you can subclass EditField and override the keyChar() method:

    _editField = new EditField() {
        protected boolean keyChar(char key, int status, int time) {
           super.keyChar(key, status, time);
           // 'key' is the most recent entered char
        }
    });
    

    or, you can implement a FieldChangeListener and listen for changes:

    _editField.setChangeListener(new FieldChangeListener() {
        public void fieldChanged(Field field, int context) {
            String text = _editField.getText();
            // 'text' is the full text contents of the EditField
        }
    });