2011-03-09 97 views
1

我試圖檢測一個標籤被點擊/指出/點擊。我來自Win32 C++編程& Java Swing,我知道這兩種方法都是採用不同的方法來註冊事件/輸入。Mosync:檢測指針/點擊

我看過教程,但找不到檢測點擊的示例。是否有一個常量的點擊,所以然後我可以檢測到keyPressEvent(即,像win32 & WM_LBUTTONDOWN)?或者我需要先註冊點擊,然後調用我自己的函數來處理點擊(如Java & .addActionListener())?

我嘗試檢測下方點擊不工作:

#include <MAUtil/Moblet.h> 
#include <MAUI/Layout.h> 
#include <MAUI/ListBox.h> 
#include <MAUI/Label.h> 
#include <MAUI/EditBox.h> 
#include <MAUI/Screen.h> 
#include <MAUtil/Environment.h> 
#include <madmath.h> 
#include <conprint.h> 


using namespace MAUtil; 
using namespace MAUI; 

class MouseScreen : public Screen, public PointerListener 
{ 
    private: 
     Label *testLabel; 
    public: 
     MouseScreen() 
     { 
      MAExtent screenDim = maGetScrSize(); 
      Layout* mainLayout = new Layout(0, 0, EXTENT_X(screenDim), EXTENT_Y(screenDim), NULL, 1, 3); 
      ListBox* mainListBox = new ListBox(0, 0, 100, 200, mainLayout, 
             ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR, 
             true); 
      mainListBox -> setPaddingLeft(10); 
      mainListBox -> setPaddingRight(10); 
      mainListBox -> setPaddingTop(10); 
      mainListBox -> setPaddingBottom(10); 
      mainListBox -> setBackgroundColor(900); 
      mainLayout -> setBackgroundColor(300); 

      testLabel = new Label(10, 300, 50, 20, mainLayout); 
      //testLabel -> addPointerListener(this); 
      testLabel -> setCaption("Click me"); 

      mainLayout -> add(testLabel); 
     } 

     void pointerPressEvent(MAPoint2d p) 
     { 
      printf("clicked"); // never occurs 

      // OR 
      if (testLabel.contains((MouseScreen*)p)) 
      { 
       printf("Label clicked"); 
      } 
      // Should I call parent function 
      // PointerListener :: pointerPressEvent(p); 
     } 

     void pointerMoveEvent(MAPoint2d p) {} 
     void pointerReleaseEvent(MAPoint2d p) {} 
}; 

class MouseMoblet : public Moblet 
{ 
    public: 
     MouseMoblet() 
     { 
      instance = new MouseScreen(); 
      instance -> show(); 
     } 

     ~MouseMoblet() 
     { 
      delete instance; 
     } 

     void keyPressEvent(int keyCode, int nativeCode) 
     { 
      // todo: handle key presses 
      printf("Blah"); // never occurs when I press the mouse, but other KEYS work 
     } 

     void keyReleaseEvent(int keyCode, int nativeCode) 
     { 
      // todo: handle key releases 
     } 

    private: 
     MouseScreen *instance; 
}; 

extern "C" int MAMain() 
{ 
    Moblet::run(new MouseMoblet()); 
    return 0; 
}; 

回答

0

我可以看到一些東西,你需要做的。首先,您需要通過調用setMain(mainLayout)將mainLayout設置爲Screen的主窗口部件。這使得屏幕知道mainLayout,以便它可以繪製它。完成此操作後,您將能夠在屏幕上看到您的小部件,並且還應該獲得點擊事件。

在pointerPressedEvent中你幾乎是正確的,testLabel的contains方法需要一個點而不是一個Screen。你應該在這裏做的是調用testLabel-> contains(p.x,p.y)來評估testLabel是否被點擊。

完整的修改後的代碼看起來是這樣的:

#include <MAUtil/Moblet.h> 
#include <MAUI/Layout.h> 
#include <MAUI/ListBox.h> 
#include <MAUI/Label.h> 
#include <MAUI/EditBox.h> 
#include <MAUI/Screen.h> 
#include <MAUtil/Environment.h> 
#include <madmath.h> 
#include <conprint.h> 


using namespace MAUtil; 
using namespace MAUI; 

class MouseScreen : public Screen 
{ 
    private: 
     Label *testLabel; 
    public: 
     MouseScreen() 
     { 
      MAExtent screenDim = maGetScrSize(); 
      Layout* mainLayout = new Layout(0, 0, EXTENT_X(screenDim), EXTENT_Y(screenDim), NULL, 1, 3); 
      ListBox* mainListBox = new ListBox(0, 0, 100, 200, mainLayout, 
             ListBox::LBO_VERTICAL, ListBox::LBA_LINEAR, 
             true); 
      mainListBox -> setPaddingLeft(10); 
      mainListBox -> setPaddingRight(10); 
      mainListBox -> setPaddingTop(10); 
      mainListBox -> setPaddingBottom(10); 
      mainListBox -> setBackgroundColor(900); 
      mainLayout -> setBackgroundColor(300); 

      testLabel = new Label(10, 300, 50, 20, mainLayout); 
      //testLabel -> addPointerListener(this); 
      testLabel -> setCaption("Click me"); 

      mainLayout -> add(testLabel); 

      setMain(mainLayout); 
     } 

     void pointerPressEvent(MAPoint2d p) 
     { 
      if (testLabel->contains(p.x, p.y)) 
      { 
       printf("Label clicked"); 
      } 
     } 

     void pointerMoveEvent(MAPoint2d p) {} 
     void pointerReleaseEvent(MAPoint2d p) {} 
}; 

class MouseMoblet : public Moblet 
{ 
    public: 
     MouseMoblet() 
     { 
      instance = new MouseScreen(); 
      instance -> show(); 
     } 

     ~MouseMoblet() 
     { 
      delete instance; 
     } 

     void keyPressEvent(int keyCode, int nativeCode) 
     { 
      // todo: handle key presses 
      printf("Blah"); // never occurs when I press the mouse, but other KEYS work 
     } 

     void keyReleaseEvent(int keyCode, int nativeCode) 
     { 
      // todo: handle key releases 
     } 

    private: 
     MouseScreen *instance; 
}; 

extern "C" int MAMain() 
{ 
    Moblet::run(new MouseMoblet()); 
    return 0; 
}; 

注:目前MoSync UI系統設計用於非觸控手機,所以它是一個有點做作處理指針的事件,然而,這將是在即將發佈的版本中有所改進

關於你應該調用父函數還是不取決於如果你想保持默認行爲或沒有,目前屏幕的默認實現什麼也不做。