gpt4 book ai didi

Javafx:使用主题标签和号码实时更新电话号码文本字段

转载 作者:行者123 更新时间:2023-12-01 08:54:22 26 4
gpt4 key购买 nike

目前正在开发一个文本字段,如果您输入号码,它会更新它,如下所示:

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

最佳答案

您不应该使用监听器,因为您正在更改正在监听的属性,这意味着监听器会被调用两次。事实上,将会导致无限循环,除非 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;
}

关于Javafx:使用主题标签和号码实时更新电话号码文本字段,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42149834/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com