How to disable emojis programmatically in Android

后端 未结 7 963
走了就别回头了
走了就别回头了 2020-12-06 02:11

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 suggesti

相关标签:
7条回答
  • 2020-12-06 02:30

    This might be helpful for you it will disable emojis android:inputType="textShortMessage" . Please let me know if it doesn't help you

    0 讨论(0)
  • 2020-12-06 02:31

    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
       }
    }
    
    0 讨论(0)
  • 2020-12-06 02:35

    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);
    
    0 讨论(0)
  • 2020-12-06 02:40

    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.

    0 讨论(0)
  • 2020-12-06 02:43

    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);
    
    0 讨论(0)
  • 2020-12-06 02:45

    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;
            }
        }
    
    0 讨论(0)
提交回复
热议问题