代号 一个事件在包含更多子容器的容器中监听

Codename One event listening within a Container which contains more sub-Containers

我正在使用 Codename One 开发移动应用程序。

我有一个外部容器,我通过调用方法 addPointerReleasedListener 添加了一个 ActionListener

还有更多未添加任何侦听器的内部容器。

举例说明:

outer-Container (added `addPointerReleasedListener` to it)
 - An Image Container (NO listener added)
 - A Text Container (NO listener added)


--------------------------------------------------------------
| |            |                                             |
| |            |                                             |
| |   Image    |  Some Text here                             |
| |            |                                             |
| |            |                                             |
--------------------------------------------------------------
                                    ^        ^        ^
                                    works only if I click/press
                                    here

我注意到,为了调用该事件,我必须 click/press 在未占用任何内部容器的区域上。也就是说,如果我在任何文本和图像区域上 click/press,它 不会 起作用(因为没有添加任何听众)。我必须在外部容器的 "empty" 区域专门 click/press。显然,这没有意义,因为我想让整个容器在用户 clicks/presses 位于其中的任何位置时以相同的方式做出反应。

什么是最好的 method/practice 来实现这个? (我发现将同一个监听器添加到多个内部容器有点多余。)

您的方法在真实设备上无法正常工作。

创建一个按钮并将您的 actionListener 添加到该按钮并将外部容器的 leadComponent 设置为该按钮。

Button btn = new Button("");
btn.addActionListener(e -> {
    //Your action here
});
outerContainer.setLeadComponent(btn);

您不必将该按钮添加到容器中,只需将其设置为 leadComponent。