I am having a problem where if i press/touch outside of a field the fieldChanged()
event is triggered for the field that has focus.
The layout for my
Just had the same issue. The main problem is that navigationClick
and trackwheelClick
are called if the touch event is not consumed in touchEvent
.
The solution is to call fieldChangeNotify
within the *Click
methods only if the click was triggered by a non-touch event. Touch events are given a status of 0 so you can check for that as follows:
protected boolean navigationClick( int status, int time ){
if (status != 0) fieldChangeNotify(0);
return true;
}
protected boolean trackwheelClick( int status, int time ){
if (status != 0) fieldChangeNotify(0);
return true;
}
This method means you don't need to track whether the touch event was within the bounds of the field.
Seems what i was doing was a bit off. I didn't need to pass the event down. That is done naturally (or unnaturally, as the Field
that has focus seems to be recieving TouchEvents
event if it hasnt been touched). What seems to be happening is that after a TouchEvent.Click
a navigationClick
is sent to the field. In navigationClick
i was calling fieldChangeNotify(0)
To fix this my Field
's touchEvent
and navigationClick
now look like this:
private boolean touchEventInside;
...
protected boolean touchEvent(TouchEvent message) {
if(message.getX(1) < 0 || message.getX(1) > getWidth() || message.getY(1) < 0 || message.getY(1) > getHeight()) {
touchEventInside = false;
return false;
}else{
touchEventInside = true;
return true;
}
}
protected boolean navigationClick(int status, int time) {
if(((myMainScreen)this.getScreen()).touchStarted){
if(touchEventInside){
fieldChangeNotify(0);
}
}else{
fieldChangeNotify(0);
}
return true;
}
I am also keeping track of touch events being started in myMainScreen
, like so:
protected boolean touchStarted;
...
protected boolean touchEvent(TouchEvent message){
if(message.getEvent() == TouchEvent.UP){
touchStarted = false;
}else if(message.getEvent() == TouchEvent.DOWN){
touchStarted = true;
}
super.touchEvent(message);
return false;
}
The trick is not to override touchEvent(TouchEvent message)
at all. :)
Just override navigationClick(int status, int time)
for the field you want to handle the click (just to be clear - no need to do this for ButtonField
- it works nice as is). The BB UI framework will call navigationClick(int status, int time)
when user clicks your field on a touch screen. As well it'll work for a non-touch screen devices.