android– 如何使用InputFilter实现数字分组输入掩码?

android– 如何使用InputFilter实现数字分组输入掩码?,第1张

概述我正在使用InputFilter类来制作支持数字分组的蒙版EditText.例如,当用户插入“12345”时,我想在EditText中显示“12,345”.我该如何实现它?这是我不完整的代码:InputFilterIF=newInputFilter(){@OverridepublicCharSequencefilter(CharSequence

我正在使用inputFilter类来制作支持数字分组的蒙版EditText.例如,当用户插入“12345”时,我想在EditText中显示“12,345”.我该如何实现它?

这是我不完整的代码:

        inputFilter IF = new inputFilter() {        @OverrIDe        public CharSequence filter(CharSequence source, int start, int end,                Spanned dest, int dstart, int dend) {            for (int i = start; i < end; i++) {                if (!Character.isLetterOrDigit(source.charat(i))) {                    return "";                }            }            if (dest.length() > 0 && dest.length() % 3 == 0)            {                return "," + source;            }            return null;        }    };    edTradius.setFilters(new inputFilter[] { IF });

有没有其他方法来实现这种输入掩码?

解决方法:

这是@vincent响应的改进.它添加了对1234 5678 9190格式中删除空格的检查,因此在尝试删除空格时,只需将光标后面的字符移动到空格前的数字即可.即使插入空格,它也会将光标保持在相同的相对位置.

    mTxtCardNumber.addTextChanged@R_403_6818@ener(new TextWatcher() {        private boolean spaceDeleted;        public voID onTextChanged(CharSequence s, int start, int before, int count) {        }        public voID beforeTextChanged(CharSequence s, int start, int count, int after) {            // check if a space was deleted            CharSequence charDeleted = s.subSequence(start, start + count);            spaceDeleted = " ".equals(charDeleted.toString());        }        public voID afterTextChanged(Editable editable) {            // disable text watcher            mTxtCardNumber.removeTextChanged@R_403_6818@ener(this);            // record cursor position as setting the text in the textvIEw            // places the cursor at the end            int cursorposition = mTxtCardNumber.getSelectionStart();            String withSpaces = formatText(editable);            mTxtCardNumber.setText(withSpaces);            // set the cursor at the last position + the spaces added since the            // space are always added before the cursor            mTxtCardNumber.setSelection(cursorposition + (withSpaces.length() - editable.length()));            // if a space was deleted also deleted just move the cursor            // before the space            if (spaceDeleted) {                mTxtCardNumber.setSelection(mTxtCardNumber.getSelectionStart() - 1);                spaceDeleted = false;            }            // enable text watcher            mTxtCardNumber.addTextChanged@R_403_6818@ener(this);        }        private String formatText(CharSequence text)        {            StringBuilder formatted = new StringBuilder();            int count = 0;            for (int i = 0; i < text.length(); ++i)            {                if (Character.isDigit(text.charat(i)))                {                    if (count % 4 == 0 && count > 0)                        formatted.append(" ");                    formatted.append(text.charat(i));                    ++count;                }            }            return formatted.toString();        }    });
总结

以上是内存溢出为你收集整理的android – 如何使用InputFilter实现数字分组输入掩码?全部内容,希望文章能够帮你解决android – 如何使用InputFilter实现数字分组输入掩码?所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/web/1109576.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-29
下一篇 2022-05-29

发表评论

登录后才能评论

评论列表(0条)

保存