目前正在处理文本字段,如果您输入您的号码,则会将其更新为:
1 ## - ### - ####
12 #### - ####
不幸的是,通过控制台将输入“1234”转换为“123-4 ## - ####”可以正常工作,但是当我按下更新后的字符串“123-4 ## - #### “它会自动将光标部分替换为不断替换第一个数字的开头。更新文本框中的字符串时,有人可以帮助保存光标位置吗?这是我的更新代码。
phoneField.textProperty().addListener((observable, oldValue, newValue) -> {
Pattern p = Pattern.compile("-?\\d+");
Matcher m = p.matcher(newValue);
String result = "";
String finalString = "";
try {
while (m.find()) {
result = m.group();
}
int size = result.length();
for(int i = 0; i < size; i++) {
if(i == 3 || i == 6) finalString += "-";
finalString += result.charAt(i);
}
for(int i = size; i < 10; i++) {
if(i == 3 || i == 6) finalString += "-";
finalString += "#";
}
} catch (Exception e) {
finalString = "INVALID NUMBER";
}
phoneField.setText(finalString);
});
答案 0 :(得分:0)
我对您的代码所做的更改很少。 首先,你应该追加匹配结果以获得正确的值 - 我已经使用了StringBuilder.append()。然后,我改变了编译模式,否则它将无法正确处理输入。
phoneField.textProperty().addListener((observable, oldValue, newValue) -> {
Pattern p = Pattern.compile("\\d+");
Matcher m = p.matcher(newValue);
StringBuilder result = new StringBuilder();
String finalString = "";
try {
while (m.find()) {
result.append(m.group());
}
int size = result.length();
for (int i = 0; i < size; i++) {
if (i == 3 || i == 6) finalString += "-";
finalString += result.charAt(i);
}
for (int i = size; i < 9; i++) {
if (i == 3 || i == 6) finalString += "-";
finalString += "#";
}
} catch (Exception e) {
finalString = "INVALID NUMBER";
}
phoneField.setText(finalString);
});
注意,可能值得为该字段添加长度检查。
答案 1 :(得分:0)
你不应该使用一个监听器,因为你正在改变你正在监听的属性,这意味着监听器被调用两次。实际上,除非StringProperty足够智能,以便在新值等于旧值时避免触发更改,否则将导致无限循环。 (大多数符合JavaBean的类都以这种方式运行,但我不知道对该行为的任何保证。)
要限制TextField的行为,通常需要使用TextFormatter:
private TextField createPhoneField() {
TextField phoneField = new TextField();
phoneField.setPrefColumnCount(12);
TextFormatter<String> formatter =
new TextFormatter<>(this::addPhoneNumberMask);
phoneField.setTextFormatter(formatter);
return phoneField;
}
private TextFormatter.Change addPhoneNumberMask(
TextFormatter.Change change) {
// Ignore cursor movements, unless the text is empty (in which case
// we're initializing the field).
if (!change.isContentChange() &&
!change.getControlNewText().isEmpty()) {
return change;
}
String text = change.getControlNewText();
int start = change.getRangeStart();
int end = change.getRangeEnd();
int anchor = change.getAnchor();
int caret = change.getCaretPosition();
StringBuilder newText = new StringBuilder(text);
int dash;
while ((dash = newText.lastIndexOf("-")) >= start) {
newText.deleteCharAt(dash);
if (caret > dash) {
caret--;
}
if (anchor > dash) {
anchor--;
}
}
while (newText.length() < 3) {
newText.append('#');
}
if (newText.length() == 3 || newText.charAt(3) != '-') {
newText.insert(3, '-');
if (caret > 3 || (caret == 3 && end <= 3 && change.isDeleted())) {
caret++;
}
if (anchor > 3 || (anchor == 3 && end <= 3 && change.isDeleted())) {
anchor++;
}
}
while (newText.length() < 7) {
newText.append('#');
}
if (newText.length() == 7 || newText.charAt(7) != '-') {
newText.insert(7, '-');
if (caret > 7 || (caret == 7 && end <= 7 && change.isDeleted())) {
caret++;
}
if (anchor > 7 || (anchor == 7 && end <= 7 && change.isDeleted())) {
anchor++;
}
}
while (newText.length() < 12) {
newText.append('#');
}
if (newText.length() > 12) {
newText.delete(12, newText.length());
}
text = newText.toString();
anchor = Math.min(anchor, 12);
caret = Math.min(caret, 12);
change.setText(text);
change.setRange(0, change.getControlText().length());
change.setAnchor(anchor);
change.setCaretPosition(caret);
return change;
}