messageContentEditText.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(Editable s) {
int nSelStart = 0;
int nSelEnd = 0;
boolean nOverMaxLength = false;
nSelStart = messageContentEditText.getSelectionStart();
nSelEnd = messageContentEditText.getSelectionEnd();
nOverMaxLength = (s.length() > MAX_MESSAGE_CHAR_LENGTH) ? true : false;
if (nOverMaxLength) {
Toast.makeText(MessageSendActivity.this,
"不能超过" + MAX_MESSAGE_CHAR_LENGTH + "字符!",
Toast.LENGTH_SHORT);
s.delete(nSelStart - 1, nSelEnd);
messageContentEditText.setTextKeepState(s);//请读者注意这一行,保持光标原先的位置,而 mEditText.setText(s)会让光标跑到最前面,
//就算是再加mEditText.setSelection(nSelStart) 也不起作用
}
charLeftTextView.setText("剩余:" + (MAX_MESSAGE_CHAR_LENGTH - messageContentEditText.getText().length()));
}
});
android 限定 EditText字符输入个数
最新推荐文章于 2024-10-24 16:42:33 发布
本文介绍了一种在Android应用中实现的消息文本输入监听机制,该机制能够有效防止用户输入超出预设的最大字符数限制,并在达到限制时给出提示。
1382

被折叠的 条评论
为什么被折叠?



