我正在处理自定义edittext,但我有点卡在一件事上,我发现TextWatcher 在自定义edittext 中不起作用。
public class InputValidation extends EditText {
public InputValidation(Context context) {
super(context);
}
public InputValidation(Context context, AttributeSet attrs) {
super(context, attrs);
}
public InputValidation(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
public void addTextChangedListener(android.text.TextWatcher watcher) {
super.addTextChangedListener(new TextWatcherDelegator());
}
public class TextWatcherDelegator implements android.text.TextWatcher {
@Override
public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
android.util.Log.d("TextWatcher", " beforeTextChanged :: " + charSequence.toString());
}
@Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
}
@Override
public void afterTextChanged(android.text.Editable editable) {
android.util.Log.d("TextWatcher", " afterTextChanged :: " + editable.toString());
}
}
}
XML 布局
<com.example.inputvalidation.InputValidation
android:id="@+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:hint="Fullname"
android:singleLine="true"
android:textSize="20sp"/>
在这段代码之上,它根本没有调用 TextWatcher 状态,请仔细阅读我的代码并建议我一些解决方案。