开发者

Hiding the keyboard when a control loses focus?

开发者 https://www.devze.com 2023-01-14 11:23 出处:网络
now I have this: public void focusChanged(Field field, int eventType) { if ( field == txtAmount && eventType == 1)

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.开发者_如何学Go

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


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);
            }
        }
    }
}


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().

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号