Live character count for EditText

后端 未结 14 2127
清歌不尽
清歌不尽 2020-11-29 17:07

I was wondering what the best way to do a live character count of an edit-text box is in Android. I was looking at this but I couldn\'t seem to make any sense of it.

<
14条回答
  •  [愿得一人]
    2020-11-29 17:44

    Clear way;

    abstract class CharacterWatcher : TextWatcher {
        override fun afterTextChanged(text: Editable?) {
            afterCharacterChanged(text?.lastOrNull(), text?.length)
        }
    
        override fun beforeTextChanged(text: CharSequence?, start: Int, count: Int, before: Int) {}
    
        override fun onTextChanged(text: CharSequence?, start: Int, before: Int, count: Int) {}
    
        abstract fun afterCharacterChanged(char: Char?, count: Int?)
    }
    
    
    
     editText.addTextChangedListener(new CharacterWatcher() {
                @Override
                public void afterCharacterChanged(@Nullable Character character, @Nullable Integer count) {
                    action()
                }
            });
    

提交回复
热议问题