Live character count for EditText

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

    This solution uses Kotlin and shows the number of characters left. Also, if the current number of characters surpasses the limit of 50, the text color will change to red.

    Kotlin

    private val mTitleTextWatcher = object : TextWatcher {
        override fun beforeTextChanged(s: CharSequence, start: Int, count: Int, after: Int) {}
    
        override fun onTextChanged(s: CharSequence, start: Int, before: Int, count: Int) {
            if(YOUR_EDIT_TEXT_ID.text.toString().trim().length < 51){
                YOUR_CHAR_LEFT_TEXTVIEW_ID.text = (50 - YOUR_EDIT_TEXT_ID.text.toString().trim().length).toString()
                YOUR_CHAR_LEFT_TEXTVIEW_ID.setTextColor(Color.BLACK)
            }
            else{
                YOUR_CHAR_LEFT_TEXTVIEW_ID.text = "0"
                YOUR_CHAR_LEFT_TEXTVIEW_ID.setTextColor(Color.RED)
            }
        }
    
        override fun afterTextChanged(s: Editable) {}
    }
    

    Also, don't forget to add the TextWatcher to your EditText

    YOUR_EDIT_TEXT_ID.addTextChangedListener(mTitleTextWatcher)
    
    0 讨论(0)
  • 2020-11-29 17:43
        You can use TextWatcher class to see text has changed and how much number of character remains.Here i have set counter of 140 characters.
    
        EditText typeMessageToPost;
        TextView number_of_character;
    public void onCreate(Bundle savedBundleInstance) {
            super.onCreate(savedBundleInstance);
    setContentView(R.layout.post_activity);
    typeMessageToPost.addTextChangedListener(mTextEditorWatcher);
    }
    private final TextWatcher mTextEditorWatcher=new TextWatcher() {
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count,
                    int after) {
                // TODO Auto-generated method stub
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                // TODO Auto-generated method stub
                number_of_character.setText(String.valueOf(140-s.length()));
            }
        };
    
    0 讨论(0)
  • 2020-11-29 17:44

    Just set these 2 lines in TextInputLayoutin your XML file:

    app:counterEnabled="true"
    app:counterMaxLength="200"
    
    0 讨论(0)
  • 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()
                }
            });
    
    0 讨论(0)
  • 2020-11-29 17:46

    You can do it with TextInputLayout and compat libraries with:

    app:counterEnabled="true"
    app:counterMaxLength="420"
    

    and complete:

    <android.support.design.widget.TextInputLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:counterEnabled="true"
        app:counterMaxLength="420">
    
        <EditText
          android:layout_width="match_parent"
          android:layout_height="wrap_content"
          android:maxLength="420" />
    
    </android.support.design.widget.TextInputLayout>
    
    0 讨论(0)
  • 2020-11-29 17:46

    in xml add this attribute for editText

        android:maxLength="80"
    

    in java add this listener

      ed_caption.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
    
            }
    
            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
    
            }
    
            @Override
            public void afterTextChanged(Editable s) {
                tv_counter.setText(80 - s.toString().length() + "/80");
    
            }
        });
    
    0 讨论(0)
提交回复
热议问题