使用“ CustomTextWatcher”的想法,我做到了
1)创建了一个新的TextWatcherListener接口:
public interface TextWatcherExtendedListener extends NoCopySpan
{
public void afterTextChanged(View v, Editable s);
public void onTextChanged(View v, CharSequence s, int start, int before, int count);
public void beforeTextChanged(View v, CharSequence s, int start, int count, int after);
}
2)创建并使用EditTextExtended而不是EditText(在我的情况下):
public class EditTextExtended extends EditText
{
private TextWatcherExtendedListener mListeners = null;
public EditTextExtended(Context context)
{
super(context);
}
public EditTextExtended(Context context, AttributeSet attrs)
{
super(context, attrs);
}
public EditTextExtended(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
}
public void addTextChangedListener(TextWatcherExtendedListener watcher)
{
if (mListeners == null)
{
mListeners = watcher;
}
}
public void removeTextChangedListener(TextWatcherExtendedListener watcher)
{
if (mListeners != null)
{
mListeners = null;
}
}
void sendBeforeTextChanged(CharSequence text, int start, int before, int after)
{
if (mListeners != null)
{
mListeners.beforeTextChanged(this, text, start, before, after);
}
}
void sendOnTextChanged(CharSequence text, int start, int before,int after)
{
if (mListeners != null)
{
mListeners.onTextChanged(this, text, start, before, after);
}
}
void sendAfterTextChanged(Editable text)
{
if (mListeners != null)
{
mListeners.afterTextChanged(this, text);
}
}
}
3)因此,在需要编写此代码的地方:
myEditTextExtended.addTextChangedListener(this)
4)使用它们:
@Override
public void onTextChanged(View v, CharSequence s, int start, int before, int count)
{
}
@Override
public void beforeTextChanged(View v, CharSequence s, int start, int count, int after)
{
}
@Override
public void afterTextChanged(View v, Editable s)
{
}
好吧,让我知道你的想法。