现在的位置: 首页 > 综合 > 正文

Android: On EditText Changed Listener

2017年12月12日 ⁄ 综合 ⁄ 共 2870字 ⁄ 字号 评论关闭

In my project I have a EditText.
I want to show how many characters are there in the EditText and
show it in the TextView.
I have written following code and it works fine. However, my problem is when I click Backspace it counts up. I need to decrement the number. What should I do? How can I take Backspace Key?

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

抱歉!评论已关闭.