我想在TextView
显示一个计数器,该计数器会减少用户在EditText中写入的字符数。
我有一个TextView
来显示110个字符的计数器,109,108,107 ......
wordCount = (TextView) findViewById(R.id.nouvelle_annonce_words_count) ;
我有一个用户写的Editext:
title = (EditText) findViewById(R.id.nouvelle_annonce_titre) ;
如何在EditText中监听更改并使用剩余字符数更新textView?
答案 0 :(得分:2)
您可以像这样使用addTextChangedListener
title.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
wordCount.setText(String.valueOf(110 - (title.getText().toString().length)));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void afterTextChanged(Editable s) {
}
});
答案 1 :(得分:0)
您要做的事情称为TextWatcher
title.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
wordCount.setText(String.valueOf(110-s.length()));
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});