Live character count for EditText

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

    you can use a TextWatcher to see when the text has changed

    private TextView mTextView;
    private EditText mEditText;
    private final TextWatcher mTextEditorWatcher = new TextWatcher() {
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }
    
            public void onTextChanged(CharSequence s, int start, int before, int count) {
               //This sets a textview to the current length
               mTextView.setText(String.valueOf(s.length()));
            }
    
            public void afterTextChanged(Editable s) {
            }
    };
    

    you set the TextWatcher for the edittext with

    mEditText.addTextChangedListener(mTextEditorWatcher);
    

提交回复
热议问题