我正在尝试检测何时单击/指向/点击标签。我来自Win32 C ++& Java Swing和我知道两种方法都采用不同的方法来注册事件/输入。
我查看了教程,但找不到检测点击的示例。点击是否有常数,那么我可以在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 :(得分:0)
我可以看到你需要做的一些事情。首先,您需要通过调用setMain(mainLayout)将mainLayout设置为Screen的主要小部件。这使得屏幕可以识别mainLayout,以便它可以绘制它。完成此操作后,您将能够在屏幕上看到您的小部件,并且还应该获得点击事件。
在pointerPressedEvent中,你几乎是正确的,testLabel的contains方法取一个点而不是一个Screen。你应该在这里做的是调用testLabel-&gt; 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系统是为非触摸手机设计的,因此处理指针事件有点人为,但是在即将发布的版本中会有所改进。
关于是否应该调用父函数取决于您是否要保留默认行为,目前Screen的默认实现不执行任何操作。