Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- dependencies{
- compile 'com.xw.repo:xedittext:2.0.0@aar'
- }
- <com.xw.repo.XEditText
- xmlns:app="http://schemas.android.com/apk/res-auto"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- app:x_disableEmoji="true"/>
- public class CustomEditText extends EditText {
- public CustomEditText(Context context) {
- super(context);
- init();
- }
- public CustomEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init();
- }
- private void init() {
- setFilters(new InputFilter[]{new EmojiExcludeFilter()});
- }
- private class EmojiExcludeFilter implements InputFilter {
- @Override
- public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
- for (int i = start; i < end; i++) {
- int type = Character.getType(source.charAt(i));
- if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
- return "";
- }
- }
- return null;
- }
- }
- }
- <EditText
- android:id="@+id/edt_note"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:hint="@string/note"
- android:inputType="textEmailAddress"
- android:padding="10dp"
- android:textColor="@color/white" />
- 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, "");
- }
- };
- }
- InputFilter[] filterArray = new InputFilter[] {getEditTextFilterEmoji()}
- editText.setFilters(filterArray);
- public class EmojiExcludeEditText extends EditText {
- public EmojiExcludeEditText(Context context) {
- super(context);
- init();
- }
- public EmojiExcludeEditText(Context context, AttributeSet attrs) {
- super(context, attrs);
- init();
- }
- public EmojiExcludeEditText(Context context, AttributeSet attrs, int defStyleAttr) {
- super(context, attrs, defStyleAttr);
- init();
- }
- private void init() {
- setFilters(new InputFilter[]{emojiExcludeFilter});
- }
- @Override
- public void setFilters(InputFilter[] filters) {
- if (filters.length != 0) { //if length == 0 it will here return when init() is called
- boolean add = true;
- for (InputFilter inputFilter : filters) {
- if (inputFilter == emojiExcludeFilter) {
- add = false;
- break;
- }
- }
- if (add) {
- filters = Arrays.copyOf(filters, filters.length + 1);
- filters[filters.length - 1] = emojiExcludeFilter;
- }
- }
- super.setFilters(filters);
- }
- private EmojiExcludeFilter emojiExcludeFilter = new EmojiExcludeFilter();
- private class EmojiExcludeFilter implements InputFilter {
- @Override
- public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
- for (int i = start; i < end; i++) {
- int type = Character.getType(source.charAt(i));
- if (type == Character.SURROGATE || type == Character.OTHER_SYMBOL) {
- return "";
- }
- }
- return null;
- }
- }
- }
- <EditText
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:digits="qwertyuiopasdfghjklzxcvbnm 1234567890 QWERTYUIOPASDFGHJKLZXCVBNM" />
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement