user interface - Blackberry Click Events -


i have imagebutton in verticalfieldmanager , vfm in horizontalfieldmanager. imagebutton click event firing when click anywhere in surrounding field. want click event fire when clicking imagebutton, not field (vfm or hfm).

following code:

imagebutton login = new imagebutton(configmodel.getloginbutton(), focusable|field.field_right, "login.png", "plogin.png",0x9cbe95); horizontalfieldmanager hfm = new horizontalfieldmanager(use_all_width); verticalfieldmanager loginbuttonvfm = new verticalfieldmanager(use_all_width);  loginbuttonvfm.add(login); hfm.add(loginbuttonvfm); add(hfm);   fieldchangelistener loginlistener = new fieldchangelistener() {  public void fieldchanged(field field, int context) {      dialog.alert("fired");     } } login.setchangelistener(loginlistener);  

imagebutton class code here:

public class imagebutton extends field{          //image button class          private string _label;         private int _labelheight;         private int _labelwidth;         private font _font;          private bitmap _currentpicture;         private bitmap _onpicture;         private bitmap _offpicture;         int color;          public imagebutton(string text, long style ,string img, string img_hvr, int color){             super(style);              _offpicture = bitmap.getbitmapresource(img);             _onpicture = bitmap.getbitmapresource(img_hvr);              _font = font.getdefault().derive(font.bold, 7, ui.units_pt);             _label = text;               _labelheight = _onpicture.getheight();               _labelwidth = _onpicture.getwidth();              this.color = color;              _currentpicture = _offpicture;         }         public void setimage(string img){              _offpicture = bitmap.getbitmapresource(img);              _currentpicture = _offpicture;         }          /**          * @return text on button          */         public void settext(string text){             _label = text;         }             string gettext(){             return _label;         }          /**          * field implementation.          * @see net.rim.device.api.ui.field#getpreferredheight()          */         public int getpreferredheight(){             return _labelheight;         }         /**          * field implementation.          * @see net.rim.device.api.ui.field#getpreferredwidth()          */         public int getpreferredwidth(){             return _labelwidth;         }          /**          * field implementation.  changes picture when focus gained.          * @see net.rim.device.api.ui.field#onfocus(int)          */         protected void onfocus(int direction) {               _currentpicture = _onpicture;           //  invalidate();             super.onfocus(direction);         }          /**          * field implementation.  changes picture when focus lost.          * @see net.rim.device.api.ui.field#onunfocus()          */         protected void onunfocus() {             _currentpicture = _offpicture;             invalidate();             super.onunfocus();         }          /**          * field implementation.            * @see net.rim.device.api.ui.field#drawfocus(graphics, boolean)          */ //      protected void drawfocus(graphics graphics, boolean on) { //          // nothing //      }         protected void drawfocus(graphics graphics, boolean on) {             if (on) {                      //draw own custom focus.                     }                 }         /**          * field implementation.          * @see net.rim.device.api.ui.field#layout(int, int)          */         protected void layout(int width, int height) {             setextent(math.min( width, getpreferredwidth()),             math.min( height, getpreferredheight()));         }         /**          * field implementation.          * @see net.rim.device.api.ui.field#paint(graphics)          */         protected void paint(graphics graphics){                   // first draw background colour , picture             graphics.setcolor(this.color);             graphics.fillrect(0, 0, getwidth(), getheight());             graphics.drawbitmap(0, 0, getwidth(), getheight(), _currentpicture, 0, 0);              // draw text             graphics.setcolor(color.white);             graphics.setfont(_font);             graphics.setfont(graphics.getfont().derive(font.bold));              graphics.drawtext(_label, 5,9,                 (int)( getstyle() & drawstyle.ellipsis | drawstyle.valign_mask | drawstyle.halign_mask),                 getwidth() - 6 );          }          /**          * overridden event dispatch thread can catch event          * instead of having caught here..          * @see net.rim.device.api.ui.field#navigationclick(int, int)          */         protected boolean navigationclick(int status, int time){             fieldchangenotify(1);             return true;         }  } 

this kind of thing pretty easy find bugs in, when try write custom fields yourself. (it's not fault ... blackberry java makes stuff more difficult should be!)

i think easiest way solve problem reuse else has tested. recommend looking @ blackberry advanced ui library, in particular, these 2 classes:

basebuttonfield

bitmapbuttonfield

as think need. believe important code need change, or add imagebutton class this:

protected boolean keychar( char character, int status, int time )  {     if( character == characters.enter ) {         clickbutton();         return true;     }     return super.keychar( character, status, time ); }  protected boolean navigationclick( int status, int time )  {     if (status != 0) clickbutton();      return true;     }  protected boolean trackwheelclick( int status, int time ) {             if (status != 0) clickbutton();         return true; }  protected boolean invokeaction( int action )  {     switch( action ) {         case action_invoke: {             clickbutton();              return true;         }     }     return super.invokeaction( action ); }      protected boolean touchevent( touchevent message ) {     int x = message.getx( 1 );     int y = message.gety( 1 );     if( x < 0 || y < 0 || x > getextent().width || y > getextent().height ) {         // outside field         return false;     }     switch( message.getevent() ) {          case touchevent.unclick:             clickbutton();             return true;     }     return super.touchevent( message ); }  /**  * public way click button  */ public void clickbutton()  {     fieldchangenotify( 0 ); } 

you able use fieldchangelistener now, image button not call unless click occurs on button.

if want, can change code above use

    fieldchangenotify( 1 ); 

if 1 meaningful context value you. important thing add other methods, , change navigationclick().


Comments

Popular posts from this blog

jquery - How can I dynamically add a browser tab? -

node.js - Getting the socket id,user id pair of a logged in user(s) -

keyboard - C++ GetAsyncKeyState alternative -