gpt4 book ai didi

java - 如何设置DocumentFilter的输入长度和范围?例如1-3 或 10-80

转载 作者:行者123 更新时间:2023-12-01 14:48:18 26 4
gpt4 key购买 nike

我使用 DocumentFilter 将输入限制为整数或小数。我在这里发布的代码对此效果很好。

任何人都可以帮助我了解如何限制给定代码中的输入长度或范围吗?

谢谢!!

class MyIntFilter extends DocumentFilter {
public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {

Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();

sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);

if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
} else {
// warn the user and don't allow the insert
}
}

private boolean test(String text) {
try {
Integer.parseInt(text);
return true;
} catch (NumberFormatException e) {
return false;
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {

Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder(2);
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);

if (test(sb.toString())) {
super.replace(fb, offset, length, text, attrs);
} else {
// warn the user and don't allow the insert
}

}

@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder(2);
sb.append(doc.getText(0, doc.getLength()));
//sb.append(doc.getText(0, 2));
sb.delete(offset, offset + length);

if (test(sb.toString())) {
super.remove(fb, offset, length);
} else {
// warn the user and don't allow the insert
}

}


}

最佳答案

您可能想测试一下(我没有),但基本想法应该可以帮助您开始。

另请查看Document Filter Examples

至于设置最小长度,您可能需要使用 InputVerifier还有

class MyIntFilter extends DocumentFilter {

private int maxLength = 0;

public void setMaxLength(int maxLength) {
this.maxLength = maxLength;
}

public int getMaxLength() {
return maxLength;
}

public void insertString(FilterBypass fb, int offset, String string,
AttributeSet attr) throws BadLocationException {

Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder();

sb.append(doc.getText(0, doc.getLength()));
sb.insert(offset, string);

if (maxLength > 0 && doc.getLength() + string.length() <= maxLength) {
if (test(sb.toString())) {
super.insertString(fb, offset, string, attr);
} else {
// warn the user and don't allow the insert
}
}
}

private boolean test(String text) {
try {
Integer.parseInt(text);
return true;
} catch (NumberFormatException e) {
return false;
}
}

@Override
public void replace(FilterBypass fb, int offset, int length, String text,
AttributeSet attrs) throws BadLocationException {

Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder(2);
sb.append(doc.getText(0, doc.getLength()));
sb.replace(offset, offset + length, text);

if (test(sb.toString())) {
if (sb.length() > maxLength) {
length = sb.length() - maxLength;
if (length > 0) {
text = text.substring(0, length);
super.replace(fb, offset, length, text, attrs);
}
}
} else {
// warn the user and don't allow the insert
}

}

@Override
public void remove(FilterBypass fb, int offset, int length)
throws BadLocationException {
Document doc = fb.getDocument();
StringBuilder sb = new StringBuilder(2);
sb.append(doc.getText(0, doc.getLength()));
//sb.append(doc.getText(0, 2));
sb.delete(offset, offset + length);

if (test(sb.toString())) {
super.remove(fb, offset, length);
} else {
// warn the user and don't allow the insert
}

}
}

关于java - 如何设置DocumentFilter的输入长度和范围?例如1-3 或 10-80,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15166921/

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