久久久久久久av_日韩在线中文_看一级毛片视频_日本精品二区_成人深夜福利视频_武道仙尊动漫在线观看

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)?的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)吧!

問題描述

限時送ChatGPT賬號..

我正在嘗試將我的 Android 項目切換到 Kotlin.我有一個 EditText(TextView 的子類),我想以編程方式為其設(shè)置提示和文本.提示按預(yù)期工作.但是,對于文本,如果我嘗試使用 Kotlin setter 語法進行操作,則會出現(xiàn)類型不匹配異常:

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

如果我們查看聲明,我們會發(fā)現(xiàn)繼承自 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,但是這兩種方法有什么區(qū)別使編譯器表現(xiàn)不同呢?我唯一能想到的是 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 是從哪里來的?沒有對應(yīng)的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 足以創(chuàng)建具有 getter 類型的合成屬性.另一方面,如果只有 setter 存在,則不會創(chuàng)建該屬性.

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.

當(dāng) setter 出現(xiàn)時,屬性創(chuàng)建變得更加困難.原因是 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 設(shè)置為 EditText.因此,Kotlin 合理地創(chuàng)建了 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.

這篇關(guān)于Kotlin 屬性訪問語法如何適用于 Java 類(即 EditText setText)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網(wǎng)!

【網(wǎng)站聲明】本站部分內(nèi)容來源于互聯(lián)網(wǎng),旨在幫助大家更快的解決問題,如果有圖片或者內(nèi)容侵犯了您的權(quán)益,請聯(lián)系我們刪除處理,感謝您的支持!

相關(guān)文檔推薦

Cut, copy, paste in android(在android中剪切、復(fù)制、粘貼)
android EditText blends into background(android EditText 融入背景)
Change Line Color of EditText - Android(更改 EditText 的線條顏色 - Android)
EditText showing numbers with 2 decimals at all times(EditText 始終顯示帶 2 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標(biāo)在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 久久久久久久久久久福利观看 | 亚洲国产成人在线视频 | 操操日 | 午夜精品91 | 成人妇女免费播放久久久 | 亚洲精品一区二区网址 | 99热最新网址 | 久久伊人一区 | 青青草免费在线视频 | 日韩免费网站 | 91视频国产精品 | 天天曰夜夜操 | 久久久久久国产 | 色爱综合网| 欧美日韩在线观看一区二区三区 | 99re视频在线 | 久久国产精品一区二区三区 | 国产日韩欧美精品一区二区三区 | 国产精品久久久久久久久动漫 | 亚洲一区二区av | 精品成人免费视频 | 国产精品日本一区二区在线播放 | 欧美精品黄 | 欧美激情va永久在线播放 | 黄色片a级| 国产精品久久久久久久久免费高清 | 黑人巨大精品欧美一区二区免费 | 欧美国产在线一区 | 亚洲欧美综合精品久久成人 | 神马影院一区二区三区 | 欧美日韩成人影院 | 少妇淫片aaaaa毛片叫床爽 | 国产91丝袜在线播放 | 日韩精品在线看 | 亚洲先锋影音 | www四虎影视 | 久久久久久久夜 | 欧美精品久久久久久久久老牛影院 | 国产一区二区在线免费视频 | 99精品视频在线 | 亚洲日本乱码在线观看 |