本文介紹了計數 EditText 中的字符已更改偵聽器的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
在我的項目中,我有一個 EditText
.我想計算 EditText
中的字符數,并在 TextView
中顯示該數字.我已經編寫了以下代碼,它工作正常.但是,我的問題是當我單擊 Backspace 時它會增加,但我需要減少數字.如何考慮 Backspace?
In my project I have an EditText
. I want to count the characters in the EditText
, and show that number it in a TextView
. I have written the following code and it works fine. However, my problem is when I click Backspace it counts up, but I need to decrement the number. How can I consider Backspace?
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){}
});
推薦答案
使用
s.length()
曾經在其中一個答案中提出以下建議,但效率很低
The following was once suggested in one of the answers, but its very inefficient
textMessage.getText().toString().length()
這篇關于計數 EditText 中的字符已更改偵聽器的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!