防止用户在 SWT 的文本字段中的后缀后输入字符
Prevent User From Entering Characters After a Suffix in a Text Field in SWT
我希望用户只在后缀前写。我怎样才能在 SWT 中做到这一点?
类似于:
public class Test {
public static void main(String[] args) {
final String SUFFIX = "end";
Display display = new Display();
Shell shell = new Shell(display);
final Text text = new Text(shell, SWT.BORDER);
text.setText(SUFFIX);
text.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
// if e.text is after SUFFIX
// e.doit = false;
}
});
}
}
像这样:
text.addVerifyListener(event -> {
// Get the result text of the edit
StringBuilder buff = new StringBuilder(text.getText());
String newText = event.text != null ? event.text : "";
buff.replace(event.start, event.end, newText);
// Allow if result still ends with suffix
event.doit = buff.toString().endsWith(SUFFIX);
});
我希望用户只在后缀前写。我怎样才能在 SWT 中做到这一点?
类似于:
public class Test {
public static void main(String[] args) {
final String SUFFIX = "end";
Display display = new Display();
Shell shell = new Shell(display);
final Text text = new Text(shell, SWT.BORDER);
text.setText(SUFFIX);
text.addVerifyListener(new VerifyListener() {
@Override
public void verifyText(VerifyEvent e) {
// if e.text is after SUFFIX
// e.doit = false;
}
});
}
}
像这样:
text.addVerifyListener(event -> {
// Get the result text of the edit
StringBuilder buff = new StringBuilder(text.getText());
String newText = event.text != null ? event.text : "";
buff.replace(event.start, event.end, newText);
// Allow if result still ends with suffix
event.doit = buff.toString().endsWith(SUFFIX);
});