pbootcms网站模板|日韩1区2区|织梦模板||网站源码|日韩1区2区|jquery建站特效-html5模板网

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 語法進行操作,則會出現(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 是從哪里來的?沒有對應的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.

當 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 設置為 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.

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

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

相關文檔推薦

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 位小數(shù)的數(shù)字)
Changing where cursor starts in an expanded EditText(更改光標在展開的 EditText 中的開始位置)
EditText, adjustPan, ScrollView issue in android(android中的EditText,adjustPan,ScrollView問題)
主站蜘蛛池模板: 爆破器材运输车|烟花爆竹运输车|1-9类危险品厢式运输车|湖北江南专用特种汽车有限公司 | 德国BOSCH电磁阀-德国HERION电磁阀-JOUCOMATIC电磁阀|乾拓百科 | 湖南成人高考报名-湖南成考网 | 鑫铭东办公家具一站式定制采购-深圳办公家具厂家直销 | 企业彩铃制作_移动、联通、电信集团彩铃上传开通_彩铃定制_商务彩铃管理平台-集团彩铃网 | 上海璟文空运首页_一级航空货运代理公司_机场快递当日达 | 冷却塔降噪隔音_冷却塔噪声治理_冷却塔噪音处理厂家-广东康明冷却塔降噪厂家 | 预制围墙_工程预制围墙_天津市瑞通建筑材料有限公司 | 西安微信朋友圈广告投放_微信朋友圈推广_西安度娘网络科技有限公司 | 衬塑管道_衬四氟管道厂家-淄博恒固化工设备有限公司 | 医学模型生产厂家-显微手术模拟训练器-仿真手术模拟训练系统-北京医教科技 | 抓斗式清污机|螺杆式|卷扬式启闭机|底轴驱动钢坝|污水处理闸门-方源水利机械 | 作文导航网_作文之家_满分作文_优秀作文_作文大全_作文素材_最新作文分享发布平台 | 硅胶管挤出机厂家_硅胶挤出机生产线_硅胶条挤出机_臣泽智能装备 贵州科比特-防雷公司厂家提供贵州防雷工程,防雷检测,防雷接地,防雷设备价格,防雷产品报价服务-贵州防雷检测公司 | 包装盒厂家_纸盒印刷_礼品盒定制-济南恒印包装有限公司 | 长江船运_国内海运_内贸船运_大件海运|运输_船舶运输价格_钢材船运_内河运输_风电甲板船_游艇运输_航运货代电话_上海交航船运 | SMC-ASCO-CKD气缸-FESTO-MAC电磁阀-上海天筹自动化设备官网 | 灌装封尾机_胶水灌装机_软管灌装封尾机_无锡和博自动化机械制造有限公司 | 气力输送_输送机械_自动化配料系统_负压吸送_制造主力军江苏高达智能装备有限公司! | 辐射仪|辐射检测仪|辐射巡测仪|个人剂量报警仪|表面污染检测仪|辐射报警仪|辐射防护网 | 蒸汽热收缩机_蒸汽发生器_塑封机_包膜机_封切收缩机_热收缩包装机_真空机_全自动打包机_捆扎机_封箱机-东莞市中堡智能科技有限公司 | 小学教案模板_中学教师优秀教案_高中教学设计模板_教育巴巴 | 税筹星_灵活用工平台_企业财务顾问_财税法薪综合服务平台 | 小型高低温循环试验箱-可程式高低温湿热交变试验箱-东莞市拓德环境测试设备有限公司 | 南方珠江-南方一线电缆-南方珠江科技电缆-南方珠江科技有限公司 南汇8424西瓜_南汇玉菇甜瓜-南汇水蜜桃价格 | CTAB,表面活性剂1631溴型(十六烷基三甲基溴化铵)-上海升纬化工原料有限公司 | 深圳VI设计-画册设计-LOGO设计-包装设计-品牌策划公司-[智睿画册设计公司] | 青州搬家公司电话_青州搬家公司哪家好「鸿喜」青州搬家 | 废气处理设备-工业除尘器-RTO-RCO-蓄热式焚烧炉厂家-江苏天达环保设备有限公司 | 郑州水质检测中心_井水检测_河南废气检测_河南中环嘉创检测 | 钢丝绳探伤仪-钢丝绳检测仪-钢丝绳探伤设备-洛阳泰斯特探伤技术有限公司 | 橡胶粉碎机_橡胶磨粉机_轮胎粉碎机_轮胎磨粉机-河南鼎聚重工机械制造有限公司 | 环境模拟实验室_液体-气体控温机_气体控温箱_无锡双润冷却科技有限公司 | 中宏网-今日新闻-财经新闻| 南京泽朗生物科技有限公司| 浇钢砖,流钢砖_厂家价低-淄博恒森耐火材料有限公司 | 电脑刺绣_绣花厂家_绣花章仔_织唛厂家-[源欣刺绣]潮牌刺绣打版定制绣花加工厂家 | 船用锚链|专业锚链生产厂家|安徽亚太锚链制造有限公司 | 变色龙PPT-国内原创PPT模板交易平台 - PPT贰零 - 西安聚讯网络科技有限公司 | 右手官网|右手工业设计|外观设计公司|工业设计公司|产品创新设计|医疗产品结构设计|EMC产品结构设计 | 工业车间焊接-整体|集中除尘设备-激光|等离子切割机配套除尘-粉尘烟尘净化治理厂家-山东美蓝环保科技有限公司 |