0

我正在开发一款用于触摸和打字手机的应用程序。现在在应用程序的一个屏幕中,有 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);
}

但它不起作用..任何人都可以帮助我吗?触摸事件应该像复选框一样在屏幕中间工作。

谢谢..这是屏幕截图

4

1 回答 1

2

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

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

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;
}
于 2012-04-12T09:38:33.773 回答