本文介绍了如何设置具有输入长度和范围的DocumentFilter?例如1-3或10-80的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用DocumentFilter将输入限制为整数或十进制.我在这里发布的代码对此非常有效.

I'm using DocumentFilter to restrict input as integer or decimal.And the code I post here is working well for that.

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

Can anybody help me about how to restrict the input length or range in the given code?

谢谢!

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
  }

}


}

推荐答案

您可能想测试一下(如我所没有),但是基本的想法应该可以帮助您入门.

You may want to test this (as I haven't), but the basic idea should get you started.

还要查看文档过滤器示例

关于设置最小长度,您可能需要使用 InputVerifier 以及

As to setting a minimum length, you may want to use a InputVerifier as well

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
        }

    }
}

这篇关于如何设置具有输入长度和范围的DocumentFilter?例如1-3或10-80的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 16:34