我正在尝试获取用户对 EditText 所做的修改,无论是插入还是删除。我使用 TextWatcher 但我没有得到正确的结果,而且有时“getChar(start, end) has end before start”错误。
editText = (EditText) findViewById(R.id.MyEditText);
editText.addTextChangedListener(new TextWatcher() {
@override
public void afterTextChanged(Editable s){}
@override
public void beforeTextChanged(CharSequence s, int start, int count, int after){
showToast("text removed: " + s.subSequence(start, count));
}
@override
public void onTextChanged(CharSequence s, int start, int before, int count){
showToast("text added: " + s.subSequence(start, count));
}
}
如您所见,我用于beforeTextChanged
获取用户删除和onTextChanged
插入的任何文本。请在这里阐明一下。谢谢!
API就在这里:http: //developer.android.com/reference/android/text/TextWatcher.html#afterTextChanged(android.text.Editable)
编辑:
我似乎想通了......这很愚蠢:s.subSequence(start, count))
真的应该s.subSequence(start, start+count))