我有几个TextField
需要在焦点丢失时执行某些操作,例如:修剪文本,将其与初始文本进行比较,检查它以及它是否有效 - 保存。哪种方式处理它会更好?通过使用ChangeListener
语句创建一个switch
并在Textfield
s中共享它:
ChangeListener<Boolean> focusHandler = new ChangeListener<Boolean>() {
private String initialValue = null;
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
ReadOnlyBooleanProperty booleanProperty = (ReadOnlyBooleanProperty) observable;
TextField textField = (TextField)booleanProperty.getBean();
if (focus) { // gained focus
initialValue = textField.getText();
} else { // lost focus
trimText(textField);
if(!textField.getText().equals(initialValue)){ //text was changed
if(textField.getText().isEmpty()){ //text was erased
switch (textField.getId()) {
case CoreConstants.AREA_SIZE_TF:
emptyAreaSize();
break;
case CoreConstants.NUMBER_OF_PEOPLE_TF:
emptyPeopleLiving();
break;
default:
//no actions
break;
}
}else{
switch (textField.getId()) {
case CoreConstants.NAME_TF:
System.out.println("Execute name code!");
break;
case CoreConstants.SURNAME_TF:
System.out.println("Execute last name code!");
break;
case CoreConstants.MAIL_TF:
System.out.println("Execute mail code!");
break;
default:
//no actions
break;
}}}}}};
nameTextField.focusedProperty().addListener(focusHandler);
surnameTextField.focusedProperty().addListener(focusHandler);
或者为每个ChangeListener
创建单独的TextField
:
numberOfPeopleTextField.focusedProperty().addListener(new ChangeListener<Boolean>() {
private String initialValue = null;
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
if (focus) { // gained focus
initialValue = numberOfPeopleTextField.getText();
} else { // lost focus
trimText(numberOfPeopleTextField);
if(!numberOfPeopleTextField.getText().equals(initialValue)){ //text was changed
if(numberOfPeopleTextField.getText().isEmpty()){
emptyPeopleLiving();
}else{
mailInfoUpdate("mail");
}
}
}
}
});
mailTextField.focusedProperty().addListener(new ChangeListener<Boolean>() {
private String initialValue = null;
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
if (focus) { // gained focus
initialValue = mailTextField.getText();
} else { // lost focus
trimText(mailTextField);
if(!mailTextField.getText().equals(initialValue)){ //text was changed
mailInfoUpdate("mail");
}
}
}
});
创建多个ChangeListener
VS一个共享ChangeListener
哪个更好用,为什么?
答案 0 :(得分:2)
我选择选项3:创建一个包含公共代码的类并使用不同的处理程序。
class FocusChangeListener implements ChangeListener<Boolean> {
private final TextField textField;
private final Consumer<? super String> changeHandler;
private String initialValue = null;
FocusChangeListener(TextField textField, Consumer<? super String> changeHandler) {
this.textField = textField;
this.changeHandler = changeHandler;
}
@Override
public void changed(ObservableValue<? extends Boolean> observable, Boolean oldValue, Boolean focus) {
if (focus) { // gained focus
initialValue = textField.getText();
} else { // lost focus
trimText(textField);
if(changeHandler != null && !textField.getText().equals(initialValue)) { //text was changed
changeHandler.accept(textField.getText());
}
}
}
}
numberOfPeopleTextField.focusedProperty().addListener(new FocusChangeListener(numberOfPeopleTextField, text -> {
if(text.isEmpty()){
emptyPeopleLiving();
} else{
mailInfoUpdate("mail");
}
}));
mailTextField.focusedProperty().addListener(new FocusChangeListener(mailTextField, text -> mailInfoUpdate("mail")));
如果它更适合您的需求,您还可以使用Consumer
中的抽象方法替换FocusChangeListener
...
启用id
是一个坏主意,因为除了使用魔术字符串之外,它不会分离问题。重新实现整个监听器也不是一个好主意,因为它使代码更难维护......