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

問題描述

限時送ChatGPT賬號..

我正在嘗試將我的 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)?的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!

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

相關文檔推薦

Cut, copy, paste in android(在android中剪切、復制、粘貼)
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 位小數的數字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 日韩在线视频一区二区三区 | 日韩在线免费电影 | 日韩精品一区在线 | 色秀网站| 国产目拍亚洲精品99久久精品 | 精品国产一区二区 | 久久精品一 | 精品国产91| 国产成人精品一区二区三区在线 | 欧一区 | 欧美一级免费片 | 中文字字幕一区二区三区四区五区 | 国精产品一区一区三区免费完 | 91精品国产一区 | 日韩亚洲视频在线 | 一区二区精品 | 97avcc| h视频免费观看 | 午夜激情影院 | 久久久久国产 | 国产 欧美 日韩 一区 | 日本高清不卡视频 | 一区二区三区亚洲 | 亚洲欧美激情国产综合久久久 | 久久一本 | 日日夜夜天天久久 | 国产精品日韩高清伦字幕搜索 | 午夜影院在线观看 | 黄视频网址 | 日韩伦理一区二区三区 | 在线视频一区二区三区 | 亚洲精品18 | 亚洲美女视频 | 欧美一区二区三区在线 | 国产精品成人一区二区三区吃奶 | 99精品久久久国产一区二区三 | 国产成人99久久亚洲综合精品 | 国产一区二区三区四区 | 久久99网站 | 成人免费在线播放 | 久久新 |