【问题标题】:Touch Event in Blackberry?黑莓中的触摸事件?
【发布时间】:2011-06-21 16:01:29
【问题描述】:

我正在开发一款触控和非触控设备的 blackerry 应用程序。我在我的应用程序中使用自定义按钮。这是我的代码

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 == 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);
    }   }

使用模拟器进行测试没有问题。但我无法导航到真实设备中的按钮。我正在使用 9780 Blackberry 粗体设备。我不知道问题出在哪里

【问题讨论】:

    标签: blackberry touch


    【解决方案1】:

    尝试执行以下操作:

    1)。删除这些东西:

     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)。确保你的领域是可聚焦的。正如文档所说:

    如果您希望您的领域收到 焦点,那么你必须覆盖 isFocusable 返回 true。

    附:实际上你不需要覆盖touchEvent(TouchEvent message)。检查我的另一个post

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多