Advertisement
Guest User

Untitled

a guest
Sep 5th, 2017
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.93 KB | None | 0 0
  1. dependencies{
  2. compile 'com.xw.repo:xedittext:2.0.0@aar'
  3. }
  4.  
  5. <com.xw.repo.XEditText
  6. xmlns:app="http://schemas.android.com/apk/res-auto"
  7. android:layout_width="match_parent"
  8. android:layout_height="wrap_content"
  9. app:x_disableEmoji="true"/>
  10.  
  11. public class CustomEditText extends EditText {
  12. public CustomEditText(Context context) {
  13. super(context);
  14. init();
  15. }
  16.  
  17. public CustomEditText(Context context, AttributeSet attrs) {
  18. super(context, attrs);
  19. init();
  20. }
  21.  
  22. public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
  23. super(context, attrs, defStyleAttr);
  24. init();
  25. }
  26.  
  27. private void init() {
  28. setFilters(new InputFilter[]{new EmojiExcludeFilter()});
  29. }
  30.  
  31. private class EmojiExcludeFilter implements InputFilter {
  32.  
  33. @Override
  34. public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
  35. for (int i = start; i < end; i++) {
  36. int type = Character.getType(source.charAt(i));
  37. if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
  38. return "";
  39. }
  40. }
  41. return null;
  42. }
  43. }
  44. }
  45.  
  46. <EditText
  47. android:id="@+id/edt_note"
  48. android:layout_width="match_parent"
  49. android:layout_height="wrap_content"
  50. android:hint="@string/note"
  51. android:inputType="textEmailAddress"
  52. android:padding="10dp"
  53. android:textColor="@color/white" />
  54.  
  55. public static InputFilter getEditTextFilterEmoji()
  56. {
  57. return new InputFilter()
  58. {
  59. @Override
  60. public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend)
  61. {
  62. CharSequence sourceOriginal = source;
  63. source = replaceEmoji(source);
  64. end = source.toString().length();
  65.  
  66. if (end == 0) return ""; //Return empty string if the input character is already removed
  67.  
  68. if (! sourceOriginal.toString().equals(source.toString()))
  69. {
  70. char[] v = new char[end - start];
  71. TextUtils.getChars(source, start, end, v, 0);
  72.  
  73. String s = new String(v);
  74.  
  75. if (source instanceof Spanned)
  76. {
  77. SpannableString sp = new SpannableString(s);
  78. TextUtils.copySpansFrom((Spanned) source, start, end, null, sp, 0);
  79. return sp;
  80. }
  81. else
  82. {
  83. return s;
  84. }
  85. }
  86. else
  87. {
  88. return null; // keep original
  89. }
  90. }
  91.  
  92. private String replaceEmoji(CharSequence source)
  93. {
  94.  
  95. String notAllowedCharactersRegex = "[^a-zA-Z0-9@#\$%\&\-\+\(\)\*;:!\?\~`£\{\}\[\]=\.,_/\\\s'\"<>\^\|÷×]";
  96. return source.toString()
  97. .replaceAll(notAllowedCharactersRegex, "");
  98. }
  99.  
  100. };
  101. }
  102.  
  103. InputFilter[] filterArray = new InputFilter[] {getEditTextFilterEmoji()}
  104. editText.setFilters(filterArray);
  105.  
  106. public class EmojiExcludeEditText extends EditText {
  107. public EmojiExcludeEditText(Context context) {
  108. super(context);
  109. init();
  110. }
  111.  
  112. public EmojiExcludeEditText(Context context, AttributeSet attrs) {
  113. super(context, attrs);
  114. init();
  115. }
  116.  
  117. public EmojiExcludeEditText(Context context, AttributeSet attrs, int defStyleAttr) {
  118. super(context, attrs, defStyleAttr);
  119. init();
  120. }
  121.  
  122. private void init() {
  123. setFilters(new InputFilter[]{emojiExcludeFilter});
  124. }
  125.  
  126. @Override
  127. public void setFilters(InputFilter[] filters) {
  128. if (filters.length != 0) { //if length == 0 it will here return when init() is called
  129. boolean add = true;
  130. for (InputFilter inputFilter : filters) {
  131. if (inputFilter == emojiExcludeFilter) {
  132. add = false;
  133. break;
  134. }
  135. }
  136. if (add) {
  137. filters = Arrays.copyOf(filters, filters.length + 1);
  138. filters[filters.length - 1] = emojiExcludeFilter;
  139. }
  140. }
  141. super.setFilters(filters);
  142. }
  143.  
  144. private EmojiExcludeFilter emojiExcludeFilter = new EmojiExcludeFilter();
  145.  
  146. private class EmojiExcludeFilter implements InputFilter {
  147.  
  148. @Override
  149. public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
  150. for (int i = start; i < end; i++) {
  151. int type = Character.getType(source.charAt(i));
  152. if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
  153. return "";
  154. }
  155. }
  156. return null;
  157. }
  158. }
  159. }
  160.  
  161. <EditText
  162. android:layout_width="match_parent"
  163. android:layout_height="match_parent"
  164. android:digits="qwertyuiopasdfghjklzxcvbnm 1234567890 QWERTYUIOPASDFGHJKLZXCVBNM" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement