Android中的控制EditText输入的最大字数
image.pngetContent为EditText的输入框
tvCount为对应计数显示的TextView
int maxTextCount = 100;
etContent.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
etContent.removeTextChangedListener(this);//**** 注意的地方
if (s.length() > maxTextCount) {
etContent.setText(s.toString().substring(0, maxTextCount));
etContent.setSelection(maxTextCount);
tvCount.setText(maxTextCount + "");
} else {
tvCount.setText(s.length() + "/" + maxTextCount + "字");
}
etContent.addTextChangedListener(this);//**** 注意的地方
}
@Override
public void afterTextChanged(Editable s) {
}
});
网友评论