直播字符计数的EditText字符、EditText

2023-09-12 07:01:29 作者:莓小汐°

我不知道该怎么做一个编辑文本框的现场字符数的最好办法就是在Android中。我一直在寻找这,但我似乎无法做任何它的意义。

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.

要说明这个问题,我有一个EditText,我试图限制到150。我可以用输入滤波器做到这一点的人物,但是我想显示右侧的文本框中低于用户拥有的字符数输入(几乎像堆栈溢出,现在做)。

To describe the problem, I have an EditText and I'm trying to limit the characters to 150. I can do this with an input filter, however I want to show right below the text box the number of characters a user has entered(Almost like stack overflow is doing right now).

如果有人可以写一个小片断例如code或点我在正确的方向我倒是AP preciate了很多。

If someone could write a small snippet of example code or point me in the right direction I'd appreciate it a lot.

推荐答案

您可以使用TextWatcher时看到的文本已更改

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) {
        }
};

mEditText.addTextChangedListener(mTextEditorWatcher);