TextWatcher接口有3个回调方法,当文本发生变化时,它们都按以下顺序调用:
beforeTextChanged(CharSequence s, int start, int count, int after)
- 在更改应用到文本之前调用。
s 参数是应用任何更改之前的文本。
start 参数是文本中更改部分开头的位置。
count 参数是自start 位置以来s 序列中更改部分的长度。
而after 参数是新序列的长度,它将替换s 序列中从start 到start+count 的部分。
您不得更改此方法中TextView 中的文本(使用myTextView.setText(String newText))。
onTextChanged(CharSequence s, int start, int before, int count)`
- 类似于
beforeTextChanged 方法,但在文本更改后调用。
s 参数是应用更改后的文本。
start 参数与beforeTextChanged 方法中的参数相同。
count参数是beforeTextChanged方法中的after参数。
而before参数就是beforeTextChanged方法中的count参数。
您不得更改此方法中TextView 中的文本(使用myTextView.setText(String newText))。
afterTextChanged(Editable s)
- 您可以通过此方法更改
TextView 中的文本。
/!\ 警告:当您更改TextView 中的文本时, TextWatcher 将再次被触发,开始无限循环。然后,您应该添加类似 boolean _ignore 的属性,以防止无限循环。
示例:
new TextWatcher() {
boolean _ignore = false; // indicates if the change was made by the TextWatcher itself.
@Override
public void afterTextChanged(Editable s) {
if (_ignore)
return;
_ignore = true; // prevent infinite loop
// Change your text here.
// myTextView.setText(myNewText);
_ignore = false; // release, so the TextWatcher start to listen again.
}
// Other methods...
}
总结:
即用型类:TextViewListener
就我个人而言,我制作了自定义文本侦听器,它为我提供了单独字符串中的 4 个部分,这对我来说使用起来更加直观。
/**
* Text view listener which splits the update text event in four parts:
* <ul>
* <li>The text placed <b>before</b> the updated part.</li>
* <li>The <b>old</b> text in the updated part.</li>
* <li>The <b>new</b> text in the updated part.</li>
* <li>The text placed <b>after</b> the updated part.</li>
* </ul>
* Created by Jeremy B.
*/
public abstract class TextViewListener implements TextWatcher {
/**
* Unchanged sequence which is placed before the updated sequence.
*/
private String _before;
/**
* Updated sequence before the update.
*/
private String _old;
/**
* Updated sequence after the update.
*/
private String _new;
/**
* Unchanged sequence which is placed after the updated sequence.
*/
private String _after;
/**
* Indicates when changes are made from within the listener, should be omitted.
*/
private boolean _ignore = false;
@Override
public void beforeTextChanged(CharSequence sequence, int start, int count, int after) {
_before = sequence.subSequence(0,start).toString();
_old = sequence.subSequence(start, start+count).toString();
_after = sequence.subSequence(start+count, sequence.length()).toString();
}
@Override
public void onTextChanged(CharSequence sequence, int start, int before, int count) {
_new = sequence.subSequence(start, start+count).toString();
}
@Override
public void afterTextChanged(Editable sequence) {
if (_ignore)
return;
onTextChanged(_before, _old, _new, _after);
}
/**
* Triggered method when the text in the text view has changed.
* <br/>
* You can apply changes to the text view from this method
* with the condition to call {@link #startUpdates()} before any update,
* and to call {@link #endUpdates()} after them.
*
* @param before Unchanged part of the text placed before the updated part.
* @param old Old updated part of the text.
* @param aNew New updated part of the text?
* @param after Unchanged part of the text placed after the updated part.
*/
protected abstract void onTextChanged(String before, String old, String aNew, String after);
/**
* Call this method when you start to update the text view, so it stops listening to it and then prevent an infinite loop.
* @see #endUpdates()
*/
protected void startUpdates(){
_ignore = true;
}
/**
* Call this method when you finished to update the text view in order to restart to listen to it.
* @see #startUpdates()
*/
protected void endUpdates(){
_ignore = false;
}
}
例子:
myEditText.addTextChangedListener(new TextViewListener() {
@Override
protected void onTextChanged(String before, String old, String aNew, String after) {
// intuitive use of parameters
String completeOldText = before + old + after;
String completeNewText = before + aNew + after;
// update TextView
startUpdates(); // to prevent infinite loop.
myEditText.setText(myNewText);
endUpdates();
}
}