问题
I want to hide emojis and auto suggestions from keyboard programmatically. Its working in some Android devices but not in all devices. here's my code for hide auto suggestions:
txtSingupemail.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_EMAIL_ADDRESS
|InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
txtSignuppwd.setInputType(InputType.TYPE_TEXT_VARIATION_FILTER | InputType.TYPE_TEXT_VARIATION_PASSWORD);
txtSignuppwd.setTransformationMethod(PasswordTransformationMethod.getInstance());
Here's the snapshot of my UI:
This is layout when user clicks signIn button. When user tap on bottom left icon which is marked red, the keyboard height goes increase due to emojis as suggestions.
See below snapshot:
Is there any way to hide those top emojis from keyboard programmatically?
回答1:
Try this it's works for me
editText.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;
}
}
回答2:
There are hundreds, if not thousands, of input method editors (a.k.a., soft keyboards) for Android.
None have to offer emoji. Those that do are welcome to offer them however they want, whenever they want.
There is no requirement for an input method editor to honor any flags that you put on the EditText
. Therefore, there is no requirement for an input method editor to offer any means of blocking emoji input. And, even if some do offer this ability, others might not, and those that do might do so via different flags.
The decision of whether to have an emoji option on the keyboard is between the developers of the keyboard and the user (who chooses the keyboard to use). You do not get a vote.
Since AFAIK emoji are just Unicode characters, and since you should be supporting Unicode characters elsewhere (e.g., Chinese glyphs), it is unclear what technical reason you would have to avoid emoji. That being said, you are welcome to attempt to filter them out of the text being entered (e.g., use TextWatcher
), if you are opposed to emoji.
回答3:
It's probably not the best solution but the code below worked for me just fine:
editText.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);
and since the password input type changes the font you just set the default typeface to it:
editText.setTypeface(Typeface.DEFAULT);
回答4:
In Kotlin, add Input filter to Edittext. I faced issues when digits and alphabets were rapidly typed. This code solves that
editText.filters = editTextAllowAlphabetsSymbols("") // Add any symbols that you wish to allow
then this
fun editTextAllowAlphabetsSymbols(symbols:String):Array<InputFilter>{
return arrayOf(AlphabetsSymbolsInputFilter(symbols))
}
and finally
class AlphabetsSymbolsInputFilter(symbols:String) : InputFilter {
private var mWordPattern: String
var mLetterPattern:String
init {
mLetterPattern = "[a-zA-Z.$symbols ]"
//mLetterPattern = "[a-zA-Z0-9.$symbols ]" // replace if alphanumeric
mWordPattern = "$mLetterPattern+"
}
override fun filter(source: CharSequence, start: Int, end: Int, dest: Spanned, dstart: Int, dend: Int): CharSequence? {
if(source == ""){
println("In backspace")
return source
}
if(source.isNotEmpty() && source.toString().matches(mWordPattern.toRegex())){
return source
}
var sourceStr = ""
if(source.isNotEmpty() && !source.toString().matches(mLetterPattern.toRegex())){
sourceStr = source.toString()
while(sourceStr.isNotEmpty() && !sourceStr.matches(mWordPattern.toRegex())){
println(" source --> $source dest ---> $dest")
if(sourceStr.last().isDigit()) {
print("Is digit ")
sourceStr = sourceStr.subSequence(0, sourceStr.length - 1).toString()
}
else if(!sourceStr.last().toString().matches(mLetterPattern.toRegex())) {
print("Is emoji or weird symbols")
sourceStr = sourceStr.subSequence(0, sourceStr.length - 1).toString()
}else
break
}
return sourceStr
}
return source
}
}
回答5:
This might be helpful for you it will disable emojis android:inputType="textShortMessage" . Please let me know if it doesn't help you
回答6:
Try This
There is nothing that will 100% disable emoji.You can only hide default emos of keyboard whatever if someone is using custom keyboard,you can't hide the emos of custom keyboard may be this will help you
editext.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL);
来源:https://stackoverflow.com/questions/40888902/how-to-disable-emojis-programmatically-in-android