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

时间:2017-01-06 05:24:42

标签: android regex android-edittext

从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, "");
            }
        };
    }

任何人都可以帮我吗?

7 个答案:

答案 0 :(得分:2)

据我了解,所有表情符号都包含/ u,所以我这样使用它

public static InputFilter[] getEmojiFilter(String blockChars) {
    return new InputFilter[]{(source, start, end, dest, dstart, dend) -> {
        String source1= StringEscapeUtils.escapeJava(source.toString());
        for (int i = start; i < end; i++) {
            if (source != null && blockChars.contains("" + source1.charAt(i))) {
                return source.subSequence(start, i);
            }
        }
        return null;
    }};
}

然后在Edittext的下面写一行

editext.setFilters(getEmojiFilter("'\"\\//\\u"));

答案 1 :(得分:1)

如果您只想在edittext中使用字母,请在edittext标记中添加此行

android:digits="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"

答案 2 :(得分:1)

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

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

答案 3 :(得分:1)

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

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

答案 4 :(得分:0)

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

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

答案 5 :(得分:0)

answer对我有用。我搜索了不同的问题,得到了答案。

答案 6 :(得分:0)

setFilters(new InputFilter [] {new EmojiExcludeFilter()}); 私有类EmojiExcludeFilter实现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;
    }
}