How do I use InputFilter to limit characters in an EditText in Android?

后端 未结 20 1179
慢半拍i
慢半拍i 2020-11-22 04:23

I want to restrict the chars to 0-9, a-z, A-Z and spacebar only. Setting inputtype I can limit to digits but I cannot figure out the ways of Inputfilter looking through the

相关标签:
20条回答
  • 2020-11-22 04:59

    It's Right, the best way to go about it to fix it in the XML Layout itself using:

    <EditText
    android:inputType="text"
    android:digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" />
    

    as rightly pointed by Florian Fröhlich, it works well for text views even.

    <TextView
    android:inputType="text"
    android:digits="0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" />
    

    Just a word of caution, the characters mentioned in the android:digits will only be displayed, so just be careful not to miss any set of characters out :)

    0 讨论(0)
  • 2020-11-22 05:00

    to prevent words in edittext. create a class that u could use anytime.

    public class Wordfilter implements InputFilter
    {
        @Override
        public CharSequence filter(CharSequence source, int start, int end,Spanned dest, int dstart, int dend) {
            // TODO Auto-generated method stub
            boolean append = false;
            String text = source.toString().substring(start, end);
            StringBuilder str = new StringBuilder(dest.toString());
            if(dstart == str.length())
            {
                append = true;
                str.append(text);
            }
            else
                str.replace(dstart, dend, text);
            if(str.toString().contains("aaaaaaaaaaaa/*the word here*/aaaaaaaa"))
            {
                if(append==true)
                    return "";
                else
                    return dest.subSequence(dstart, dend);
            }
            return null;
        }
    }
    
    0 讨论(0)
  • 2020-11-22 05:01

    If you subclass InputFilter you can create your own InputFilter that would filter out any non-alpha-numeric characters.

    The InputFilter Interface has one method, filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend), and it provides you with all the information you need to know about which characters were entered into the EditText it is assigned to.

    Once you have created your own InputFilter, you can assign it to the EditText by calling setFilters(...).

    http://developer.android.com/reference/android/text/InputFilter.html#filter(java.lang.CharSequence, int, int, android.text.Spanned, int, int)

    0 讨论(0)
  • 2020-11-22 05:05

    This simple solution worked for me when I needed to prevent the user from entering empty strings into an EditText. You can of course add more characters:

    InputFilter textFilter = new InputFilter() {
    
    @Override
    
    public CharSequence filter(CharSequence c, int arg1, int arg2,
    
        Spanned arg3, int arg4, int arg5) {
    
        StringBuilder sbText = new StringBuilder(c);
    
        String text = sbText.toString();
    
        if (text.contains(" ")) {    
            return "";   
        }    
        return c;   
        }   
    };
    
    private void setTextFilter(EditText editText) {
    
        editText.setFilters(new InputFilter[]{textFilter});
    
    }
    
    0 讨论(0)
  • 2020-11-22 05:07

    This is how I created filter for the Name field in Edit Text.(First letter is CAPS, and allow only single space after every word.

    public void setNameFilter() {
        InputFilter filter = new InputFilter() {
            @RequiresApi(api = Build.VERSION_CODES.KITKAT)
            public CharSequence filter(CharSequence source, int start, int end,
                                       Spanned dest, int dstart, int dend) {
                for (int i = start; i < end; i++) {
                    if (dend == 0) {
                        if (Character.isSpaceChar(source.charAt(i)) ||
                                !Character.isAlphabetic(source.charAt(i))) {
                            return Constants.Delimiter.BLANK;
                        } else {
                            return String.valueOf(source.charAt(i)).toUpperCase();
                        }
                    } else if (Character.isSpaceChar(source.charAt(i)) &&
                            String.valueOf(dest).endsWith(Constants.Delimiter.ONE_SPACE)) {
                        return Constants.Delimiter.BLANK;
                    } else if ((!Character.isSpaceChar(source.charAt(i)) &&
                            !Character.isAlphabetic(source.charAt(i)))) {
                        return Constants.Delimiter.BLANK;
                    }
                }
                return null;
            }
        };
        editText.setFilters(new InputFilter[]{filter, new InputFilter.LengthFilter(Constants.Length.NAME_LENGTH)});
    }
    
    0 讨论(0)
  • 2020-11-22 05:09

    I have done something like this to keep it simple:

    edit_text.filters = arrayOf(object : InputFilter {
        override fun filter(
            source: CharSequence?,
            start: Int,
            end: Int,
            dest: Spanned?,
            dstart: Int,
            dend: Int
        ): CharSequence? {
            return source?.subSequence(start, end)
                ?.replace(Regex("[^A-Za-z0-9 ]"), "")
        }
    })
    

    This way we are replacing all the unwanted characters in the new part of the source string with an empty string.

    The edit_text variable is the EditText object we are referring to.

    The code is written in kotlin.

    0 讨论(0)
提交回复
热议问题