views:

41

answers:

2

now I have this:

public void focusChanged(Field field, int eventType) {
    if ( field == txtAmount && eventType == 1)
    {
        getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE);
    }           
}

Now my problem is that the keyboard isn't hiding. I think the error is in the eventType parameter. What number identifies a LostFocus event? I hard coded in '1' for tests but it doesnt seem to work.

FocusChangeListener focusListener;

    //In the constructor:
    txtAmount = new EditField(Field.FIELD_RIGHT);
    txtAmount.setFocusListener(focusListener);

public void focusChanged(Field field, int eventType) {
    if ( field == txtAmount && eventType == 1)
    {
            Dialog.alert("iasdi");
        getVirtualKeyboard().setVisibility(VirtualKeyboard.HIDE);
    }           
}

why isn't this working? is there an enum or something that i can use to choose what eventType I should react to?

Also, if I remove the event type (so that the code executed regardless of the action just when focus changes right? nothing happens the dialog I put in for show, doesn't display meaniing the event is never entered. Any suggestions?

thanks

A: 

For matching the eventType, try using the constants defined in FocusChangeListener instead of hard coding "1". In this case, you probably want to use FocusChangeListener.FOCUS_LOST.

As for the case of the code not running, are you actually setting the value of the "focusListener" variable? From the code you posted, you aren't and it will just be passing as "null" into setFocusListener().

Marc Novakowski
Yeah that makes sense, how would I 'set a value for focusListener' though?
Serg
FocusChangeListener focusListener = new FocusChangeListener() -- although it's more common to just use an anonymous class in the setFocusListener() call.
Marc Novakowski
+1  A: 

eventType can be one of next constants, declared in FocusChangeListener class:

public static final int FOCUS_GAINED = 1; public static final int FOCUS_CHANGED = 2; public static final int FOCUS_LOST = 3;

!!! Use FOCUS_LOST = 3 instead of FOCUS_GAINED = 1 to handle focus lost event.

Also, check for null getVirtualKeyboard() method returning value, because it returns null on touch devices without virtual keyboard (like Bold 9700).

public static void hideVirtualKeyboard() {
    if (net.rim.device.api.ui.VirtualKeyboard.isSupported()) {
        Screen screen = UiApplication.getUiApplication().getActiveScreen();
        if (null != screen) {
            net.rim.device.api.ui.VirtualKeyboard vk = screen
                    .getVirtualKeyboard();
            if (vk != null) {
                vk.setVisibility(net.rim.device.api.ui.VirtualKeyboard.HIDE);
            }
        }
    }
}
Yeti