首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >Mosync:检测指针/点击

Mosync:检测指针/点击
EN

Stack Overflow用户
提问于 2011-03-10 07:13:04
回答 1查看 784关注 0票数 1

我正在尝试检测标签何时被点击/指向/点击。我来自于Win32、C++和Java Swing的编程,我知道这两种语言在注册事件/输入时采用了不同的方法。

我看过教程,但我找不到一个检测点击的例子。有没有一个表示点击的常量,这样我就可以在keyPressEvent (比如win32 & WM_LBUTTONDOWN)中检测到它了?或者我需要首先注册点击,然后调用我自己的函数来处理点击(就像Java & .addActionListener())?

我尝试检测下面的点击不起作用:

代码语言:javascript
复制
#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;
};
EN

回答 1

Stack Overflow用户

发布于 2011-03-11 23:49:55

我可以看到一些你需要做的事情。首先,您需要通过调用setMain( mainLayout )将mainLayout设置为Screen的主小部件。这使屏幕能够识别mainLayout,以便可以绘制它。一旦你做到了这一点,你将能够在屏幕上看到你的小部件,你也应该得到点击事件。

在pointerPressedEvent中,您几乎是对的,testLabel的contains方法接受一个点,而不是一个屏幕。您在这里应该做的是调用testLabel->contains( p.x,p.y )来评估是否单击了testLabel。

修改后的完整代码如下所示:

代码语言:javascript
复制
#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的默认实现什么都不做。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5253485

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档