問題描述
我正在嘗試將我的 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()
被視為普通方法而不是setter,但是這兩種方法有什么區別使編譯器表現不同呢?我唯一能想到的是 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.getter 足以創建具有 getter 類型的合成屬性.另一方面,如果只有 setter 存在,則不會創建該屬性.
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.
當 setter 出現時,屬性創建變得更加困難.原因是 getter 和 setter 可能有不同的類型.此外,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
code> 并將 Editable
設置為 EditText
.因此,Kotlin 合理地創建了 Editable
類型的合成屬性 text
.String
類不是 Editable
,這就是為什么不能將 String
實例分配給 text
屬性的原因code>EditText
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)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!