【问题标题】:Components inside List in CodenameOneCodenameOne 中 List 内的组件
【发布时间】:2014-02-04 12:39:55
【问题描述】:

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

这张图片更好地解释了我所指的内容:

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

【问题讨论】:

    标签: java list codenameone


    【解决方案1】:

    是的,但它需要一些手动编码,并且仅适用于触摸(因为您无法为其分配焦点)。

    我们通常建议在这些情况下只使用组件/容器层次结构而不是处理列表,但显然这并不总是实用的。

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

    如果您在 GUI 构建器中,这很容易:

    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 方法。因此,假设您在渲染器中有一个组件,只需向它添加一个动作侦听器。在动作监听器中只需设置一个标志,例如:

    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()
    }
    

    【讨论】:

      【解决方案2】:
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-06-08
      • 1970-01-01
      • 2021-08-26
      • 1970-01-01
      • 2021-07-31
      相关资源
      最近更新 更多