How to disable emojis programmatically in Android

后端 未结 7 1041
走了就别回头了
走了就别回头了 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: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{
            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
       }
    }
    

提交回复
热议问题