Kotlin属性访问语法如何用于Java类(即EditText setText)? [英] How does Kotlin property access syntax work for Java classes (i.e. EditText setText)?
问题描述
我正在尝试将我的Android项目切换到Kotlin.我有一个EditText
(TextView
的子类),我想以编程方式设置提示和文本.提示按预期方式工作.但是,对于文本,如果尝试使用Kotlin setter语法进行操作,则会遇到类型不匹配的异常:
I'm trying to switch my Android project to Kotlin. I have an EditText
(a subclass of TextView
) for which I want to set a hint and text programmatically. The hint works as expected. For text, though, I'm getting a type mismatch exception if I try to do it using Kotlin setter syntax:
val test = EditText(context)
test.setHint("hint") // Lint message: "Use property access syntax"
test.hint = "hint" // ok
test.setText("text") // ok (no lint message)
test.text = "text" // Type mismatch: inferred type is kotlin.String but android.text.Editable! was expected
如果看一下声明,我们会发现从TextView
继承的相同签名:
If we look at the declaration, we'll find identical signatures inherited from TextView
:
public final void setHint(CharSequence hint)
public final void setText(CharSequence text)
我给人的印象是x.y = z
是x.setY(z)
的快捷方式,但是显然这种印象是错误的. setText()
被当作普通方法而不是二传手,但是这两种方法之间的区别是什么使编译器的行为有所不同?我唯一能想到的是TextView
具有mHint
属性,但我认为情况并非如此.
I had an impression that x.y = z
was a shortcut for x.setY(z)
but apparently that impression was wrong. setText()
is treated as a normal method rather than a setter, but what's the difference between these two methods that makes the compiler behave differently? The only one I can think of is that TextView
has an mHint
property but I don't think it might be the case.
我不太了解的另一件事是,android.text.Editable
是从哪里来的?没有相应的setText(Editable)
方法,也没有这种类型的公共字段.
Another thing I don't quite understand is, where does android.text.Editable
come from? There is no corresponding setText(Editable)
method, nor is there a public field of this type.
推荐答案
为Java getter/setter对生成综合属性时,Kotlin首先会寻找getter.该吸气剂足以创建具有该吸气剂类型的合成属性.另一方面,如果仅显示设置器,则不会创建该属性.
When generating a synthetic property for a Java getter/setter pair Kotlin first looks for a getter. The getter is enough to create a synthetic property with a type of the getter. On the other hand the property will not be created if only a setter presents.
当二传手开始比赛时,财产创造变得更加困难.原因是getter和setter可能具有不同的类型.而且,可以在子类中覆盖获取器和/或设置器.
When a setter comes into play property creation becomes more difficult. The reason is that the getter and the setter may have different type. Moreover, the getter and/or the setter may be overridden in a subclass.
在您的情况下,TextView
类包含一个getter CharSequence getText()
和一个setter void setText(CharSequence)
.如果您具有类型为TextView
的变量,则您的代码可以正常工作.但是您有一个EditText
类型的变量.并且EditText
类包含重写的getter Editable getText()
,这意味着您可以为EditText
获取Editable
并将Editable
设置为EditText
.因此,Kotlin合理地创建了类型为Editable
的合成属性text
. String
类不是Editable
,这就是为什么您不能将String
实例分配给EditText
类的text
属性的原因.
In your case the TextView
class contains a getter CharSequence getText()
and a setter void setText(CharSequence)
. If you had a variable of type TextView
your code would work fine. But you have a variable of type EditText
. And the EditText
class contains an overridden getter Editable getText()
, which means that you can get an Editable
for an EditText
and set an Editable
to an EditText
. Therefore, Kotlin reasonably creates a synthetic property text
of type Editable
. The String
class is not Editable
, that's why you cannot assign a String
instance to the text
property of the EditText
class.
这篇关于Kotlin属性访问语法如何用于Java类(即EditText setText)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!