本文介绍了如何为CodenameOne TextField中的每次更改获取事件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用CodenameOne构建应用程序,并希望使用TextField过滤一长串项目。我尝试使用DataChangedListener,如 https://www.codenameone .com / javadoc / com / codename1 / ui / TextField.html ,但这只会在用户完成编辑(即按 enter键)后触发一个事件。

I am building an app using CodenameOne and want to use a TextField to filter a long list of items. I tried using a DataChangedListener, as proposed in https://www.codenameone.com/javadoc/com/codename1/ui/TextField.html, but this will only fire an event after the user completes editting (i.e. presses the 'enter' key). How can I get an event for each character changed in the text field?

推荐答案

DataChangedListener 会为每个键输入触发,而不是在按下 Enter 键时触发(这是针对 actionListener 的)。

DataChangedListener does fire for every key input and not when Enter key is pressed (this is for actionListener).

private String filterString = "";

...


filterTextField.addDataChangedListener((evt1, evt2) -> {
    if (filterTextField.getText().trim().length() > 0 && !filterString.equalsIgnoreCase(filterTextField.getText().trim())) {
        //perform your filter here
        //revalidate your form
    }
    filterString = filterTextField.getText().trim();
});

这篇关于如何为CodenameOne TextField中的每次更改获取事件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-28 07:31