lmw
2023-06-02 26c4d02ceffc158452fdf7b1894813a178259de1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
package com.fanghua.driver.utils.inputFilter;
 
import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextUtils;
 
import java.util.regex.Matcher;
import java.util.regex.Pattern;
 
/**
 * author:Created by ZHT on 2017/12/25 10:38
 * email:526309416@qq.com
 * desc:姓名
 */
public class RealNameInputFilter implements InputFilter {
 
    /**
     * @param source 新输入的字符串
     * @param start  新输入的字符串起始下标,一般为0
     * @param end    新输入的字符串终点下标,一般为source长度-nim_message_audio_playing_right_blue_bg
     * @param dest   输入之前文本框内容
     * @param dstart 原内容起始坐标,一般为0
     * @param dend   原内容终点坐标,一般为dest长度-nim_message_audio_playing_right_blue_bg
     * @return 输入内容
     */
 
    @Override
    public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
        String sourceText = source.toString();
        String destText = dest.toString();
 
        //验证删除等按键
        if (TextUtils.isEmpty(sourceText.trim())) {
            return "";
        }
        String regexStr = "[`~!@#$%^&*()+=|{}':;',\\[\\].<>/?~!@#¥%……& ;*()——+|{}【】\"\'?《》‘;:”“’。,、?\\\\]";
        Pattern pattern = Pattern.compile(regexStr);
        Matcher matcher = pattern.matcher(sourceText);
 
        Pattern emoji = Pattern.compile(
                "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",
                Pattern.UNICODE_CASE | Pattern.CASE_INSENSITIVE);
        Matcher emojiMatcher = emoji.matcher(source);
        if (matcher.matches() || emojiMatcher.find()) {
            return "";
        }
        return dest.subSequence(dstart, dend) + sourceText;
    }
}