et1.addTextChangedListener(new TextWatcher() {
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub
}
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
// TODO Auto-generated method stub
}
@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
}
});
public class CustomWatcher implements TextWatcher {
private boolean mWasEdited = false;
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
if (mWasEdited){
mWasEdited = false;
return;
}
// get entered value (if required)
String enteredValue = s.toString();
String newValue = "new value";
// don't get trap into infinite loop
mWasEdited = true;
// just replace entered value with whatever you want
s.replace(0, s.length(), newValue);
}
}
beforeTextChanged(CharSequence s, int start, int count, int after)
称为 之前的更改已经应用于文本。 s参数是 之前的短信,应用任何更改。 start参数是文本中更改部分开头的 位置。 count参数是 s序列中从 start位置开始改变的部分的 length。
And the after parameter is the 新序列的长度 which will replace the part of the s sequence from start to start+count.
从这个方法(通过使用 myTextView.setText(String newText))获得 TextView中的文本 不能改变。
onTextChanged(CharSequence s, int start, int before, int count)`
类似于 beforeTextChanged方法,但称为 之后的文本更改。 s参数是已应用的 the text after更改。 start参数与 beforeTextChanged方法中的参数相同。 count参数是 before TextChanged 方法中的 after参数。
而 before参数是 before TextChanged 方法中的 count参数。
从这个方法(通过使用 myTextView.setText(String newText))获得 TextView中的文本 不能改变。
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...
}
/**
* 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();
}
}
dialog.getInputEditText().addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence charSequence, int start, int before, int count) {
}
@Override
public void onTextChanged(CharSequence charSequence, int start, int before, int count) {
if (start<2){
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
}else{
double size = Double.parseDouble(charSequence.toString());
if (size > 0.000001 && size < 0.999999){
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(true);
}else{
ToastHelper.show(HistoryActivity.this, "Size must between 0.1 - 0.9");
dialog.getActionButton(DialogAction.POSITIVE).setEnabled(false);
}
}
}
@Override
public void afterTextChanged(Editable editable) {
}
});
package com.mindef.idttpda.shared;
// dependencies
import android.text.Editable;
import android.text.TextWatcher;
/**
* text watcher is converted to abstract class because
* then not all methods have to be overridden.
*/
public abstract class TextObserver implements TextWatcher {
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void afterTextChanged(Editable editable) {}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
}
package com.mindef.idttpda.shared;
// dependencies
import android.text.Editable;
import android.text.TextWatcher;
/**
* text watcher is converted to abstract class because
* then not all methods have to be overridden.
*/
public abstract class TextObserver implements TextWatcher {
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
public void afterTextChanged(Editable editable) {}
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
}