开发者

Touch Event in Blackberry?

I am developing one blackerry application both touch and non-touch device. I am using custom button in my app. This is my code

package CustomControls;

import net.rim.device.api.ui.*; import net.rim.device.api.ui.component.*; import net.rim.device.api.ui.container.*; import net.rim.device.api.system.*;

public class ImageButton extends Field {
    private int width;
    private int height;
    private Bitmap focusImage;   
    private Bitmap unfocusImage;   
    private boolean focusFlag=false;
    private Bitmap image;
    private String label;
    private Font font;
    public ImageButton()
    {
    }
    public ImageButton(Bitmap focusImage,Bitmap unfocusImage,int width,int height,long style)
    {
        super(style);
        label="";
        this.focusImage=focusImage;
        this.unfocusImage=unfocusImage;
        image=unfocusImage;
        this.width=width;
        this.height=height;                
    }
    public ImageButton(String label,Font font,Bitmap focusImage,Bitmap unfocusImage,int width,int height,long style)
    {
        super(style);
        this.label=label;
        this.font=font;
        this.focusImage=focusImage;
        this.unfocusImage=unfocusImage;
        image=unfocusImage;
        this.width=width;
        this.height=height;                
    }
    public int getPreferredHeight() 
    {    
          return height;
    }  
    public int getPreferredWidth() 
    {      
        return width;
    }   
    protected void onFocus(int direction) 
    {      
       image=focusImage;          
       invalidate();       
    }
    protected void onUnfocus() 
    {
        image=unfocusImage;
        invalidate();
    }
    public void setChangeImage(Bitmap fImage,Bitmap uImage)
    {
        focusImage=fImage;
        unfocusImage=uImage;
        image=fImage;
        invalidate();
    }
    protected void drawFocus(Graphics graphics, boolean on) 
    {
    } 
    protected void layout(int width, int height) 
    {
        setExtent(Math.min( width, getPreferredWidth()),Math.min(height, getPreferredHeight()));
    }

    protected void paint(Graphics graphics) 
    {            
        graphics.drawBitmap(0, 0, getWidth(),getHeight(), image, 0, 0); 
        if(label.length()>0)
        {
            graphics.setFont(font);
            graphics.drawText(label,(width-(label.length()*2))/2,(height-font.getHeight()));
        }
       }                 
                                                                                               protected boolean navigationClick(int status, int time) 
    {
        fieldChangeNotify(1);
        return true;
    }    
    protected void fieldChangeNotify(int context) 
    {
        try 
        {
            this.getChangeListener().fieldChanged(this,context);
        }

        catch (Exception exception) 
        {
             System.out.println("==> Exception in Touch "+exception.toString());
        }
    }      
     protected boolean navigationMovement(int dx, int dy, int status,int time) 
    {               
        return true;
    }    
    protected boolean touchEvent(TouchEvent message) 
    {                      
        if (TouchEvent.CLICK 开发者_如何学C== message.getEvent()) 
        {
                FieldChangeListener listener = getChangeListener();
                if (null != listener)
                        listener.fieldChanged(this, 1);
        }
        return super.touchEvent(message);            

    } 
    protected boolean trackwheelRoll(int dir, int status, int time) 
    {        
        return true;
    }     
    public void setBounds(int xPosition,int yPosition)
    {
        FieldPosition.setXPosition(this,xPosition);
        FieldPosition.setYPosition(this,yPosition);
    }   }

I don't have problem in testing by using Simulator. But i am not able to navigate to button in Real device. I am using 9780 Blackberry bold device. I dont know where the problem occur


Try to do the following:

1). Remove this stuff:

 protected boolean navigationMovement(int dx, int dy, int status,int time) 
{               
    return true;
}    

protected boolean trackwheelRoll(int dir, int status, int time) 
{        
    return true;
} 

2). Make sure your field IS focusable. As the docs say:

If you want your field to receive the focus, then you must override isFocusable to return true.

P.S. Actually you don't need to override the touchEvent(TouchEvent message). Check my another post on this.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜