android 屏蔽emoji表情

韩鸿波
2023-12-01

要在edittext屏蔽emoji 只需要拦截输入就OK了 et_set_name是自己定义的edittext

et_set_name.setFilters(new InputFilter[]{new EmojiFilter()});

public class EmojiFilter implements InputFilter {

   private String content;
    Pattern pattern = Pattern.compile( "[\ud83c\udc00-\ud83c\udfff]|[\ud83d\udc00-\ud83d\udfff]|[\u2600-\u27ff]",  Pattern . UNICODE_CASE | Pattern . CASE_INSENSITIVE );
   
    public EmojiFilter(String content) {
      super();
      this.content = content;
   }
   
   
    public EmojiFilter() {
      
   }


   @Override
   public CharSequence filter(CharSequence sourse, int strat, int end,
         Spanned dest, int dstart, int dend) {
        Matcher matcher = pattern.matcher(sourse);
         if(matcher.find()  ){
              return "" ;
          }
         return null;
    }
   
    
}
 

ok 就是这样

 类似资料: