问题
Most implementations of the text
inputType (other than URI, password, etc.) for EditText and TextView allow Emoji - although in most Google keyboard configurations this button is hidden. Is there a way to disable Emoji from being entered in an EditText? Is there an inputType parameter that could be paired with textMultiLine
that would disable Emoji?
回答1:
Modify build.gradle file, add XEditText to your project:
dependencies{
compile 'com.xw.repo:xedittext:2.0.0@aar'
}
after that, in your layout.xml:
<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"/>
Or:
Customize EditText like this:
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;
}
}
}
Both will work well !
回答2:
There is tricky way for disabling emoji from keyboard..
you just have to set
android:inputType="textEmailAddress"
for EditText
..
<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" />
I am not sure that it will work in all cases but in my case it worked for me...
回答3:
There is value digits
available in xml file for EditText. You can set there all acceptable chars.
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:digits="qwertyuiopasdfghjklzxcvbnm 1234567890 QWERTYUIOPASDFGHJKLZXCVBNM" />
I know, it's not the best solution but works :)
回答4:
Code from @woxingxiao works great, until you specify any inputType in your xml, for example android:inputType="textMultiLine"
.
I changed a little bit his proposal and I think it works great.
public class EmojiExcludeEditText extends EditText {
private EmojiExcludeFilter emojiExcludeFilter;
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() {
if (emojiExcludeFilter == null) {
emojiExcludeFilter = new EmojiExcludeFilter();
}
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 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;
}
}
}
回答5:
You can write input filter for blacklist/whitelist characters
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, "");
}
};
}
Then set it as EditText filters;
InputFilter[] filterArray = new InputFilter[] {getEditTextFilterEmoji()}
editText.setFilters(filterArray);
回答6:
There is nothing that will 100% disable emoji. The keyboard can act to any mode it sees in whatever way it thinks best, so there is no setting that will prevent emoji. If you must prevent it, do it by white or blacklisting characters with a TextWatcher.
回答7:
Add this emoji filter class:
public class EmojiFilter {
public static InputFilter[] getFilter()
{
InputFilter EMOJI_FILTER = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
for (int index = start; index < end; index++) {
int type = Character.getType(source.charAt(index));
if (type == Character.SURROGATE || type==Character.NON_SPACING_MARK
|| type==Character.OTHER_SYMBOL) {
return "";
}
}
return null;
}
};
return new InputFilter[]{EMOJI_FILTER};
}
}
And to disable emoji in edit text, do this:
editText.setFilters(EmojiFilter.getFilter());
There were some emoji which were able to type so i added:
type==Character.NON_SPACING_MARK || type==Character.OTHER_SYMBOL
in the if condition.
回答8:
You can use input filter for removing un-wanted characters, Here I used ascci values for filtering.
public class CustomEditText extends AppCompatEditText {
private InputFilter unwantedCharacterFilter;
public CustomEditText(Context context) {
super(context);
}
public CustomEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
private void init() {
setFilters(new InputFilter[]{});
}
private InputFilter getUnwantedCharacterFilter() {
if (null == unwantedCharacterFilter) {
unwantedCharacterFilter = new InputFilter() {
@Override
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) {
if (!TextUtils.isEmpty(source)) {
for (int index = start; index < end; index++) {
if (source.charAt(index) < 0 || source.charAt(index) > 177) {
return "";
}
}
}
return null;
}
};
}
return unwantedCharacterFilter;
}
@Override
public void setFilters(InputFilter[] filters) {
List<InputFilter> filterList = new ArrayList<>(Arrays.asList(filters));
filterList.add(getUnwantedCharacterFilter());
InputFilter specifiedFilters[] = filterList.toArray(new InputFilter[]{});
super.setFilters(specifiedFilters);
}
}
回答9:
Another solution:
mEtMessageText.setInputType(InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS | InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
In this example it shows the ENTER button
InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS is equivalent to android:inputType="textEmailAddress" in XML layout
回答10:
Try android:inputtype="textUri|textMultiLine"
.
In this parameter, the keyboard will change the voice input button to '/' button.
来源:https://stackoverflow.com/questions/22990870/how-to-disable-emoji-from-being-entered-in-android-edittext