我正在尝试使用以下内容通过 AppCompatEditText 创建自定义 EditText:
class MyField(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.editTextStyle,
defStyleRes: Int = 0
) : AppCompatEditText(context, attrs, defStyleAttr, defStyleRes) {
constructor(context: Context) : this(context, null, 0, 0)
constructor(context: Context, attrs: AttributeSet?) : this(context, attrs, R.attr.editTextStyle, 0)
constructor(context: Context, attrs: AttributeSet?, defStyleAttr: Int) : this(context, attrs, defStyleAttr, 0)
}
这给了我这个错误:
None of the following functions can be called with the arguments supplied:
public constructor AppCompatEditText(p0: Context) defined in androidx.appcompat.widget.AppCompatEditText
public constructor AppCompatEditText(p0: Context, p1: AttributeSet?) defined in androidx.appcompat.widget.AppCompatEditText
public constructor AppCompatEditText(p0: Context, p1: AttributeSet?, p2: Int) defined in androidx.appcompat.widget.AppCompatEditText
将鼠标悬停在下方有红线的
AppCompatEditText
上,会给出相同的消息——但不是突出显示任何可能丢失的参数,而是每个参数的右括号以红色突出显示。不确定这是否只是一个视觉错误或与我的问题有关。
我在
AppCompatEditText
定义中为每个构造函数重载定义了一个构造函数重载——我错过了什么可能导致这种情况?
我尝试在每个构造函数之后定义块,将参数定义为可为空,并确保每个重载在调用时都定义了特定值——所有这些都会导致相同的错误。
EditText 的兼容版本没有带有 4 个参数的构造函数。您只能传递上下文、属性集和默认样式。 此外,您可以使用带有
JvmOverloads
注释的简单语句,而不是手动构造函数声明:
class MyField @JvmOverloads(
context: Context,
attrs: AttributeSet? = null,
defStyleAttr: Int = R.attr.editTextStyle,
) : AppCompatEditText(context, attrs, defStyleAttr, defStyleRes) {
}