亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

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

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

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

簡直不要太方便!

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

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

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

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

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

companion object {
 fun newInstance(): TestKotlinFragment {

 val args = Bundle()

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

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

gif圖太長了,再來個靜態(tài)的:

下面是 template text,需要的直接復制。。:

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

最后再來個 kotlin 中使用的效果:

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

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

相關文章

最新評論