【发布时间】:2014-01-27 03:47:03
【问题描述】:
我正在尝试获取用户对 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 进行插入。请在这里阐明一下。谢谢!
编辑:
我好像弄明白了...这很傻:s.subSequence(start, count)) 真的应该是s.subSequence(start, start+count))
【问题讨论】:
标签: java android charsequence