How to get an event for every change in a CodenameOne TextField

倖福魔咒の 提交于 2020-01-23 01:34:10

问题


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?


回答1:


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


来源:https://stackoverflow.com/questions/46441768/how-to-get-an-event-for-every-change-in-a-codenameone-textfield

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!