使用"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) //Let implement TextWatcherExtendedListener methods
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)
{
}
好的,请让我知道您的想法。