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
Highlighted
New Developer
New Developer
Posts: 15
Registered: ‎12-03-2009
My Device: Not Specified

touchEvent problem

hi,

 

i'm implementing touchEvent for ButtonField. when the button gets focus and if i clicked the button. TouchEvent  method is executing.

 

my problem is, when button gets focus if i click any where in the screen it is executing TouchEvent .

 

TouchEvent  should execute only if i'm clicking the button region.

 

how to overcome this sutiation.

 

i attached my code with post.

 

import net.rim.device.api.ui.Color;
import net.rim.device.api.ui.Graphics;
import net.rim.device.api.ui.TouchEvent;
import net.rim.device.api.ui.component.ButtonField;
import net.rim.device.api.ui.component.Dialog;
import net.rim.device.api.ui.container.MainScreen;

public class ButtonMainScreen extends MainScreen {

	private boolean isFocus = false;
	public ButtonMainScreen() {
		ButtonField buttonField = new ButtonField("Click Me", FIELD_HCENTER | FOCUSABLE){
			protected void onFocus(int direction) {
				super.onFocus(direction);
				isFocus = true;
				invalidate();
			}

			protected void onUnfocus() {
				super.onUnfocus();
				isFocus = false;
				invalidate();
			}
			protected void paint(Graphics graphics) {
				if(isFocus){
					graphics.setColor(Color.WHITE);
				}else{
					graphics.setColor(0x666666);
				}
				super.paint(graphics);
			}
			protected boolean touchEvent(TouchEvent event) {
				int eventCode = event.getEvent();
				if (eventCode == TouchEvent.UP) {
					if (!this.isFocus()) {
						this.setFocus();
						invalidate();
					}
					return true;
				} else if (this.isFocus() && eventCode == TouchEvent.UNCLICK) {
					int choice = Dialog.ask(Dialog.D_YES_NO,
	  						"You clicked me");
	  				if (choice == Dialog.YES) {
	  					System.out.println("Clicked");
	  				}
	  				return true;
				}
				return super.touchEvent(event);
			}
		};
		add(buttonField);
	}
	
}

 

Regards,

VSk

 

Developer
Posts: 466
Registered: ‎03-04-2009
My Device: 8520
My Carrier: some

Re: touchEvent problem

well, as a thought , try either

when creating ButtonField apply also ButtonField.CONSUME_CLICK style

or

play with return values from 

protected boolean touchEvent(TouchEvent event)
Got resolved! Press kudo icon!
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: touchEvent problem

Looking at your code, there doesn't appear to be anywhere else in the Screen that focus can go, so the click event will hit your ButtonField.

 

What happens if you replace this:

 

add(buttonField);

 

this this

 

add(new NullField());

add(buttonField);

add(new NullField());