Live character count for EditText

后端 未结 14 2172
清歌不尽
清歌不尽 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:50

    You can do character counting from xml itself using TextInputLayout wrapper for EditText introduced in SupportLibrary v23.1

    Just wrap your EditText with a TextInputLayout and set CounterEnabled to true and Set a counterMaxLength.

    
        
    
    

    You'll get a material effect like this

    You may use counterOverflowTextAppearance , counterTextAppearance to style the counter.

    EDIT

    From Android documentation.

    The TextInputEditText class is provided to be used as a child of this layout. Using TextInputEditText allows TextInputLayout greater control over the visual aspects of any text input. An example usage is as so:

         
    
         
    
     
    

    TextInputLayout TextInputEditText

提交回复
热议问题