栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

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

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

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

您可能想测试一下(我还没有测试过),但是基本思路应该可以帮助您入门。

同时查看文档过滤器示例

至于设置的最小长度,你可能想使用

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


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/425861.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号