问题
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