private EditText editText;//内容
int num = 140;//限制的最大字数
editText.addTextChangedListener(new TextWatcher() { //现在输入文字数
private CharSequence temp;
private int selectionStart;
private int selectionEnd;
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
} public void onTextChanged(CharSequence s, int start, int before, int count) {
temp = s;
}
public void afterTextChanged(Editable s) {
int number = num - s.length();
String dd=number+"";
selectionStart = editText.getSelectionStart();
selectionEnd = editText.getSelectionEnd();
if (temp.length() > num) {
s.delete(selectionStart - 1, selectionEnd);
int tempSelection = selectionEnd;
editText.setText(s);
editText.setSelection(tempSelection);//设置光标在最后
}
}
}
);
相关文章: