I want to count the number of characters in EditText.

Asked 1 years ago, Updated 1 years ago, 136 views

I have EditText in my project and I want to count the number of characters in EditText. And I wrote the code below to print that number in TextView. It works well. If I type in backspace, the number of characters should decrease, but the backspace is recognized as a text Rather, the number of characters increases. How shall I do it?

tv = (TextView)findViewById(R.id.charCounts);
textMessage = (EditText)findViewById(R.id.textMessage);
textMessage.addTextChangedListener(new TextWatcher(){
    public void afterTextChanged(Editable s) {
        i++;
        tv.setText(String.valueOf(i) + " / " + String.valueOf(charCounts));
    }
    public void beforeTextChanged(CharSequence s, int start, int count, int after){}
    public void onTextChanged(CharSequence s, int start, int before, int count){}
}); 

android listener android-edittext onchange

2022-09-21 18:22

1 Answers

s.length() Try it this way. And the text view is textMessage.getText().toString().Change it to length() like this.


2022-09-21 18:22

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.