Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
Contributor
spradeesh
Posts: 39
Registered: ‎03-20-2011
My Device: 9500
My Carrier: Simulator
Accepted Solution

Multiple TouchEvent in a screen

Hi 

 

           I am using 4 horizontalFieldManagers, each containing a touchEvent method. At the runtime, whenever i click anyof the 4 horizontalFieldManagers, it is running the 1st HorizontalFieldManagers' TouchEvent. It is not triggering the appropriate TouchEvent.

 

 

HorizontalFieldManager hfm;
hfm=new HorizontalFieldManager(Field.FOCUSABLE)
                {
                    
                protected boolean touchEvent(TouchEvent message) {
                
                   int eventCode = message.getEvent();       
                  if(eventCode == TouchEvent.CLICK) {
                                   UiApplication.getUiApplication().pushScreen(new Login());
                                   }
                            return true;     
                    }   
                        
                };
vfm.add(hfm);
vfm.add(new SeparatorField());
hfm=new HorizontalFieldManager(Field.FOCUSABLE)
                {
                    
               protected boolean touchEvent(TouchEvent message) {
                
                            int eventCode = message.getEvent();       
                                if(eventCode == TouchEvent.CLICK) {
                                   UiApplication.getUiApplication().pushScreen(new Login1());
                                   }
                            return true;     
                    }   
                        
                };
vfm.add(hfm);
vfm.add(new SeparatorField());
hfm=new HorizontalFieldManager(Field.FOCUSABLE)
                {
                    
                 protected boolean touchEvent(TouchEvent message) {
                
                            int eventCode = message.getEvent();       
                                if(eventCode == TouchEvent.CLICK) {
                                   UiApplication.getUiApplication().pushScreen(new Login2());
                                   }
                            return true;     
                    }   
                        
                };
vfm.add(hfm);
vfm.add(new SeparatorField());
hfm=new HorizontalFieldManager(Field.FOCUSABLE)
                {
                    
               protected boolean touchEvent(TouchEvent message) {
                
                            int eventCode = message.getEvent();       
                                if(eventCode == TouchEvent.CLICK) {
                                   UiApplication.getUiApplication().pushScreen(new Login3());
                                   }
                            return true;     
                    }   
                        
                };
vfm.add(hfm);
vfm.add(new SeparatorField());

 

 

 

     From the above code, it always triggers the "Login()" method. Anyhow plz help.. Thanks in advance.

 

Please use plain text.
Developer
arkadyz
Posts: 2,268
Registered: ‎07-08-2009
My Device: various
My Carrier: various

Re: Multiple TouchEvent in a screen

You have more than one serious problem with your code.

1) Returning true unconditionally (instead of false or, better yet, super.touchEvent(message)) is totally wrong. You can return true for compound events such as CLICK or MOVE, but you must return super.touchEvent in all other cases - this allows the system to properly process DOWN and UP events which are parts of TAP, MOVE, CLICK, SWIPE_*, etc.

2) You never check the X and Y of the event - in fact, any touch happening outside of the active Field will still generate a touchEvent for it. You need to check whether the Field / Manager was actually touched. The surest way to do it is this:

 

int x = message.getX(1);
int y = message.getY(1);

if (x >= 0 && x < getWidth() && y >= 0 && y < getHeight()) {
  // the user touched your field - do something
  if (message.getEvent() == TouchEvent.CLICK) { // don't forget to check the event type!
    // perform your action
    // for example, pushScreen, save, etc.
    return true;
  } // can add else if for other events if needed
}
return super.touchEvent(message); // This is extremely important!

Good luck!

 

----------------------------------------------------------
please click 'Accept Solution' on posts that provide the solution to the question you've posted. Don't say "Thanks", press 'Like' button instead!
Please use plain text.