Kotlin 属性访问语法如何适用于 Java 类(即 EditTex

How does Kotlin property access syntax work for Java classes (i.e. EditText setText)?(Kotlin 属性访问语法如何适用于 Java 类(即 EditText setText)?)

本文介绍了Kotlin 属性访问语法如何适用于 Java 类(即 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 = zx.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(),这意味着您可以为 EditTextEditablecode> 并将 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)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:Kotlin 属性访问语法如何适用于 Java 类(即 EditTex

基础教程推荐