欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Kotlin之自定義 Live Templates詳解(模板代碼)

 更新時(shí)間:2020年03月17日 11:47:23   作者:wdeo3601  
這篇文章主要介紹了Kotlin之自定義 Live Templates詳解(模板代碼),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧

想必大家都知道 android studio 的 live templates 功能,那真是各種方便,比如你想使用 newInstance 去生成一個(gè)類(lèi)的實(shí)例:

簡(jiǎn)直不要太方便!

當(dāng)今 kotlin 流行起來(lái)了,你在使用 kotlin 開(kāi)發(fā) android 的時(shí)候,是不是發(fā)現(xiàn)以前用的 logt、loge、newinstance…,這些快捷創(chuàng)建代碼片段的方式都不能用了,沒(méi)關(guān)系,來(lái)一起自定義吧年輕人~

打開(kāi) android studio >> File >> Settings ,搜索 “l(fā)ive templates”:

來(lái)看看我們之前在 java 代碼里用的 newinstance 到底是怎么定義的:

現(xiàn)在,我們就可以照著葫蘆畫(huà)瓢,來(lái)把 newinstance 搬到 kotlin 里邊了:

首先先明確我們最終要生成的代碼:

companion object {
 fun newInstance(): TestKotlinFragment {

 val args = Bundle()

 val fragment = TestKotlinFragment()
 fragment.arguments = args
 return fragment
 }
}

然后就可以照著 java 版的 newinstance 模板去寫(xiě) kotlin 版的了:

gif圖太長(zhǎng)了,再來(lái)個(gè)靜態(tài)的:

下面是 template text,需要的直接復(fù)制。。:

companion object {
 fun newInstance($args$): $fragment$ {
 $nullChecks$
 val args = android.os.Bundle()
 $addArgs$
 val fragment = $fragment$()
 fragment.arguments = args
 return fragment
 }
}

最后再來(lái)個(gè) kotlin 中使用的效果:

到這里就結(jié)束了,如果你想自定義其他的,照著這個(gè)方法就可以,本文就不演示其他的了。

以上這篇Kotlin之自定義 Live Templates詳解(模板代碼)就是小編分享給大家的全部?jī)?nèi)容了,希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論