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

我正在使用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.addTextChangedListener(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.removeTextChangedListener(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.addTextChangedListener(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性能优化——之控件的优化 前面讲了图像的优化,接下...
前言 上一篇已经讲了如何实现textView中粗字体效果,里面主要...
最近项目重构,涉及到了数据库和文件下载,发现GreenDao这个...
WebView加载页面的两种方式 一、加载网络页面 加载网络页面,...
给APP全局设置字体主要分为两个方面来介绍 一、给原生界面设...
前言 最近UI大牛出了一版新的效果图,按照IOS的效果做的,页...