触摸事件无效

时间:2012-04-12 08:47:38

标签: blackberry

我正在开发一款适用于触控和手机的应用程序。现在,在应用程序的一个屏幕中,有3个按钮。

如果我触摸三个按钮,它就可以了。但是如果我触摸按钮外面,意味着在顶部栏或底部栏,那么触摸事件也在工作。假设在下面,“添加更多”按钮是重点。因此,如果我在底部栏中触摸,那么按钮触摸也在工作。为了解决这个问题,我写了下面的代码:

protected boolean touchEvent(TouchEvent event) {
    try {

        int touchXPos = event.getX(1);
        int touchYPos = event.getY(1);
        int addBtnXPos = btnAddMore.getLeft();
        int saveBtnXPos = btnSave.getLeft();
        int helpBtnXpos = btnHelp.getLeft();

        int vfmTitleTouch = m_vfmTitle.getHeight();
if(event.getEvent() == TouchEvent.CLICK)
      {
        if ((touchXPos >= addBtnXPos + 40) && (touchXPos <= (addBtnXPos + btnAddMore.getWidth() + 40)) && (touchYPos <= screenHeight -10) && (touchYPos >= (screenHeight -10 - btnAddMore.getHeight())) /*&& (touchYPos < (screenHeight-gmYPos)) */) {
            Logger.out("touchEvent", "------------------------------1");
            showPopUp();

        } else if ((touchXPos >= saveBtnXPos + 40) && (touchXPos <= (saveBtnXPos + 40 + btnSave.getWidth())) && (touchYPos <= screenHeight -10) && (touchYPos >= (screenHeight -10 - btnSave.getHeight())) /* && (touchYPos < (screenHeight-gmYPos))*/) {
            Logger.out("touchEvent", "------------------------------2");
            saveToDb();

        } else if ((touchXPos >= helpBtnXpos) && (touchXPos <= (helpBtnXpos + btnHelp.getWidth())) && (touchYPos <= (btnHelp.getTop() + btnHelp.getHeight())) && (touchYPos >= btnHelp.getTop()) /* && (touchYPos < (screenHeight-gmYPos))*/ ) {
            Logger.out("touchEvent", "------------------------------3");
            UiApplication.getUiApplication().pushScreen(new HelpScreen());

        } else if ((touchYPos <= screenHeight - hfmBtn.getHeight()) && (touchYPos >= (vfmTitleTouch))) {
            Logger.out("touchEvent", "------------------------------4");
            //Logger.out("touchEvent", "touchY::::" +touchYPos  + "Vfm Title::::" +vfmTitleTouch  + "  "+"GM Y Pos"+  gmYPos);

        } 
            return true;
        }
    } catch (Exception e) {
    }

    return super.touchEvent(event);
}

但它没有用。任何人都可以帮我解决? 触摸事件应该在屏幕中间工作,如复选框也是..

谢谢.. This is the screen shot of the screen

1 个答案:

答案 0 :(得分:2)

检查BlackBerry touchEvent outside Field triggers fieldChangedPaul Sylliboy提供的问题Arhimed的答案。

我先将这些链接粘贴在评论上,但后来将其从那里删除,以便共享代码片段以杀死不需要的触摸事件,我觉得这非常有用。

protected boolean touchEvent(TouchEvent message) {
    int event = message.getEvent();
    // you can add other events 
    if (event == TouchEvent.CLICK || event == TouchEvent.UNCLICK) {
        // Kill all click events that happen outside any fields
        if (getFieldAtLocation(message.getX(1), message.getY(1)) == -1)         
            return true;
    }

    return super.touchEvent(message);
}

// The regular getFieldAtLocation returns wrong values in 
// open spaces in complex managers, so we override it
public int getFieldAtLocation(int x, int y) {
    XYRect rect = new XYRect();
    int index = getFieldCount() - 1;
    while (index >= 0) {
        getField(index).getExtent(rect);
        if (rect.contains(x, y))
            break;
        --index;
    }

    return index;
}