我制作了自定义视图并添加了 TextWatcher,但 onTextChanged 方法也在 TextView 上。当我覆盖它时,它适用于 2 个超类。 是否可以只覆盖一个超类?
class PhoneEditText(context: Context, attrs: AttributeSet?) : AppCompatEditText(context, attrs!!),
TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, start: Int, before: Int, count: Int) {
Timber.d("onTextChanged count = $count")
}
override fun afterTextChanged(editable: Editable) {}
您继承了 AppCompatEditText 并实现了 TextWatcher 接口,这意味着当您在自定义类中重写 onTextChanged 等方法时,您正在扩展 AppCompatEditText 类和 TextWatcher 接口的行为,实际上您是在为超类/接口重写它们您正在扩展/实施。
class PhoneEditText(context: Context, attrs: AttributeSet?) : AppCompatEditText(context, attrs!!),
TextWatcher {
private var phoneTextWatcher: TextWatcher? = null
init {
phoneTextWatcher = object : TextWatcher {
override fun beforeTextChanged(charSequence: CharSequence, i: Int, i1: Int, i2: Int) {}
override fun onTextChanged(charSequence: CharSequence, start: Int, before: Int, count: Int) {
Timber.d("PhoneEditText onTextChanged count = $count")
}
override fun afterTextChanged(editable: Editable) {}
}
addTextChangedListener(phoneTextWatcher)
}
// Rest of your class implementation...
}