mEditText.addTextChangedListener(new TextWatcher() {
private CharSequence temp;
private int editStart;
private int editEnd;
private boolean isFrist = true;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
temp = s;
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
editStart = edChangeData.getSelectionStart();
editEnd = edChangeData.getSelectionEnd();
if (temp.length() > 10) {
if (isFrist) {
Toast.makeText(activity,
"你输入的字数已经超过了限制!", Toast.LENGTH_SHORT)
.show();
isFrist = false;
}
s.delete(editStart - 1, editEnd);
int tempSelection = editStart;
edChangeData.setText(s);
edChangeData.setSelection(tempSelection);
}
}
});
网友评论