CodenameOne中的List内的组件 [英] Components inside List in CodenameOne

查看:70
本文介绍了CodenameOne中的List内的组件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的问题是.是否可以在列表组件内添加类似按钮的组件(按钮具有单击时触发的功能)?

my question is. Is possible to add a component like a button (button has a functionality that triggered when it is clicked) inside a list component?

此图像更好地解释了我所指的内容:

This image explain better what I refer:

http://2.bp.blogspot .com/-HThpKcgDyRA/URI_FdpffMI/AAAAAAAAAUI/SficZAPXaCw/s1600/1.png

推荐答案

是的,但是它需要一些手写编码,并且只能用于触摸操作(因为您将无法为其分配焦点).

Yes but it requires some handcoding and it will only work for touch (since you won't be able to assign focus to it).

对于这些情况,我们通常建议仅使用组件/容器层次结构,而不要处理列表,但这显然并不总是可行的.

We normally recommend just using Component/Container hierarchies for these cases rather than dealing with lists but obviously this isn't always practical.

关键是始终使用列表操作侦听器来触发事件,仅此而已.因此,当您使用列表的动作处理代码时,您想知道它是否由您的按钮触发了...

The key is to always use the list action listener to trigger events, nothing else. So when you are in the action handling code of the list you would want to know if it was triggered by your button...

如果您在GUI生成器中,这很简单:

If you are in the GUI builder this is pretty easy:

Button b = ((GenericListCellRenderer)list.getRenderer()).extractLastClickedComponent();
if(b != null && b == myButton) {
   // your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}

手工编码的方法与一个主要警告非常相似,您没有extractLastClickedComponent方法.因此,假设您在渲染器中有一个组件,只需向其添加一个动作侦听器.在动作监听器中只需设置一个标志,例如:

The handcoded approach is pretty similar with one major caveat, you don't have the extractLastClickedComponent method. So assuming you have a component within the renderer just add an action listener to it. Within the action listener just set a flag e.g.:

myButton.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent ev) {
       buttonWasClicked = true;
    }
});

// within the list listener we do the exact same thing:
if(buttonWasClicked) {
   // for next time...
   buttonWasClicked = false;

   // your event code here for the button, the selected entry is list.getSelectedItem()/Index()
}

这篇关于CodenameOne中的List内的组件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆