EditText限制输入特殊字符、单引号、双引号和表情符号

时间:2022-06-25 21:44:39

I have a problem while inserting special characters , single quote , double quote and emojis into the database from editText. So I want my editText to restrict those characters. I got success in restricting emojis, but I failed to restrict single quote and double quote. Eg When I try to enter text Today's List from editText into database it generates an exception. I have used InputFilter in editText to filter emojis, and I want this filter to restrict single quote and double quotes too.

我有一个问题,插入特殊字符,单引号,双引号和emojis从editText进入数据库。我想要我的editText来限制这些字符。我成功地限制了表情符号,但是我没有限制单引号和双引号。当我尝试从editText输入文本到数据库时,它会产生一个异常。我在editText中使用InputFilter来过滤emojis,我希望这个过滤器也限制单引号和双引号。

public static InputFilter getEditTextFilterEmoji() {
        return new InputFilter() {

            @Override
            public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

                CharSequence sourceOriginal = source;
                source = replaceEmoji(source);
                end = source.toString().length();

                if (end == 0)
                    return ""; //Return empty string if the input character is already removed

                if (!sourceOriginal.toString().equals(source.toString())) {
                    char[] v = new char[end - start];
                    TextUtils.getChars(source, start, end, v, 0);

                    String s = new String(v);

                    if (source instanceof Spanned) {
                        SpannableString sp = new SpannableString(s);
                        TextUtils.copySpansFrom((Spanned) source, start, end, null, sp, 0);
                        return sp;
                    } else {
                        return s;
                    }
                } else {
                    return null; // keep original
                }
            }

            private String replaceEmoji(CharSequence source) {

                String notAllowedCharactersRegex = "[^a-zA-Z0-9@#\\$%\\&\\-\\+\\(\\)\\*;:!\\?\\~`£\\{\\}\\[\\]=\\.,_/\\\\\\s'\\\"<>\\^\\|÷×]";
                return source.toString()
                        .replaceAll(notAllowedCharactersRegex, "");
            }
        };
    }

Can anyone help me with this ?

有人能帮我一下吗?

6 个解决方案

#1


1  

If you want alphabets only in edittext, Add this line in edittext tag

如果只希望在edittext中使用alphabets,请在edittext标记中添加这一行

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

#2


1  

Try this one....worked for me

试试这个....为我工作

private EditText InputText;
private String blockCharacterSet = "~#^|$%&*!"; //Special characters to block

private InputFilter filter = new InputFilter() {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        if (source != null && blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InputText = (EditText) findViewById(R.id.InputText);
    InputText.setFilters(new InputFilter[] { filter });
}

#3


1  

Restrict the EditText to use given digits only.

限制EditText仅使用给定的数字。

            <EditText
            ........
            android:inputType="textPersonName"
            android:digits="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY zZ0123456789"
            />

#4


0  

Add This in Strings.xml. It's allow u to enter alphanumeric and space also.

添加这个Strings.xml。它允许你输入字母数字和空格。

<string name="charset">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY Z</string>

#5


0  

This answer worked for me. I searched different questions and I got my answer.

这个回答对我起作用了。我搜索了不同的问题,得到了答案。

#6


0  

setFilters(new InputFilter[]{new EmojiExcludeFilter()}); private class EmojiExcludeFilter implements InputFilter {

setFilters(新InputFilter[]{新EmojiExcludeFilter()});私有类emojiexclusive defilter实现InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,.:-'';§£¥...";
        for (int i = start; i < end; i++) {
            int type = Character.getType(source.charAt(i));
            if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL || type == Character.MATH_SYMBOL || specialChars.contains("" + source)||  Character.isWhitespace(0)) {
                return "";
            }
        }
        return null;
    }
}

#1


1  

If you want alphabets only in edittext, Add this line in edittext tag

如果只希望在edittext中使用alphabets,请在edittext标记中添加这一行

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

#2


1  

Try this one....worked for me

试试这个....为我工作

private EditText InputText;
private String blockCharacterSet = "~#^|$%&*!"; //Special characters to block

private InputFilter filter = new InputFilter() {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {

        if (source != null && blockCharacterSet.contains(("" + source))) {
            return "";
        }
        return null;
    }
};

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    InputText = (EditText) findViewById(R.id.InputText);
    InputText.setFilters(new InputFilter[] { filter });
}

#3


1  

Restrict the EditText to use given digits only.

限制EditText仅使用给定的数字。

            <EditText
            ........
            android:inputType="textPersonName"
            android:digits="aAbBcCdDeEfFgGhHiIjJkKlLmMnNoOpPqQrRsStTuUvVwWxXyY zZ0123456789"
            />

#4


0  

Add This in Strings.xml. It's allow u to enter alphanumeric and space also.

添加这个Strings.xml。它允许你输入字母数字和空格。

<string name="charset">0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXY Z</string>

#5


0  

This answer worked for me. I searched different questions and I got my answer.

这个回答对我起作用了。我搜索了不同的问题,得到了答案。

#6


0  

setFilters(new InputFilter[]{new EmojiExcludeFilter()}); private class EmojiExcludeFilter implements InputFilter {

setFilters(新InputFilter[]{新EmojiExcludeFilter()});私有类emojiexclusive defilter实现InputFilter {

    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String specialChars = "/*!@#$%^&*()\"{}_[]|\\?/<>,.:-'';§£¥...";
        for (int i = start; i < end; i++) {
            int type = Character.getType(source.charAt(i));
            if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL || type == Character.MATH_SYMBOL || specialChars.contains("" + source)||  Character.isWhitespace(0)) {
                return "";
            }
        }
        return null;
    }
}