BlackBerry click outside Field

2019-08-08 20:31发布

问题:

I'm implementing a custom ImageButton for touch enabled devices (9500,9550,9800,...) I have problem that click(touch) outside field generates event in focused field.(when extending Field, BitmapField)

I can solve it by moving focus to empty field, but this is not very nice. Strange thing is that this behaviour is for Field, BitmapField but not for ButtonField. It realy seems that when is ButtonField focused, outside clicks don't generates button event.

I tryed extending ButtonField, but I couldn't get rid of that stupid Button Background.

So my question; what is that difference in behavior between Field and ButtonField that causes generating events outside Field?

this is how I removed button background:

    // cahange button border
    setBorder(BorderFactory
            .createSimpleBorder(new XYEdges(0, 0, 0, 0)));
    setBorder(VISUAL_STATE_ACTIVE, BorderFactory
            .createSimpleBorder(new XYEdges(0, 0, 0, 0)));

回答1:

You just need to add a check in your touchEvent() for the ImageButton

protected boolean touchEvent(TouchEvent message) {
    //make sure the touch is withing the bounds of our Field
    if(message.getX(1) < 0 || message.getX(1) > getWidth() || message.getY(1) < 0 || message.getY(1) > getHeight()) {
        return false;
    }

    //Do your work
}

The touch event is sent to the focused Field even if it isn't actually on the Field, you have to return false so the containing Manager knows to send it to the next Field that will accept it (the Field where the touch is on, or nothing if it is on empty space).

Edit: To remove the button background, override protected void applyTheme() {}