2012-04-12 178 views
0

我developng用於觸摸和類型的電話一個應用。現在在應用程序的一個屏幕中,有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

回答

2

檢查由Paul SylliboyArhimed提供的問題BlackBerry touchEvent outside Field triggers fieldChanged的答案。

我已經貼在評論這些鏈接第一,但後來刪除它從那裏分享代碼片段殺死不想要的觸摸事件,我發現非常有用的。

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; 
} 
+0

謝謝Rupak。我也對代碼做了一些修改。但它以某種方式工作。現在,當我在按鈕外部單擊時,它不會執行與按鈕相關的代碼,而只會聚焦到按鈕。這意味着當我可以在保存按鈕外面觸摸時,焦點將進入保存按鈕。 – 2012-04-12 10:27:42

+0

我有一個簡單的查詢。你是否爲這些'ButtonField'設置了任何'FieldChangeListener'?如果是的話,除了殺死不需要的事件外,你不必在'touchEvent(..)'中做任何事情。而且我不確定可能導致焦點問題的原因。您可以按照上面提供的答案鏈接中描述的策略。 – Rupak 2012-04-12 10:44:45

+0

是的。我正在使用fieldChange Listener作爲按鈕。 – 2012-04-12 10:49:30