快捷键:
Ctr+shift+A : 打开action 输入框,输入”convert java file to kotlin”
Ctr+shift+alt+k: 将java文件转化成kotlin文件
TODO 没看懂这里activity.hello.setText(“”)
If we want to call the synthetic properties on View (useful in adapter classes), we should also importkotlinx.android.synthetic.main.activity_main.view.*.
Once we do that, we can then invoke the corresponding extensions, which are properties named after the views in the XML file. For example, for this view:
<TextView
android:id="@+id/hello"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello World, MyActivity"
/>
There will be property named hello:
activity.hello.setText("Hi!")
差异化打包: http://blog.youkuaiyun.com/myliuyx/article/details/52223066 productflavor
TODO Android flavor这里也没看懂
Android Extensions plugin supports Android flavors. Suppose you have a flavor named free in yourbuild.gradle file:
android {
productFlavors {
free {
versionName "1.0-free"
}
}
}
So you can import all synthetic properties for the free/res/layout/activity_free.xml layout by adding this import:
import kotlinx.android.synthetic.free.activity_free.*
TODO 方法在类继承间的使用
Kotlin Android Extensions is a plugin for the Kotlin compiler, and it does two things:
Adds a hidden caching function and a field inside each Kotlin Activity. The method is pretty small so it doesn't increase the size of APK much.
Replaces each synthetic property call with a function call.
How this works is that when invoking a synthetic property, where the receiver is a Kotlin Activity/Fragment class that is in module sources, the caching function is invoked. For instance, given
class MyActivity: Activity()fun MyActivity.a() {
this.textView.setText(“”)
}
a hidden caching function is generated inside MyActivity, so we can use the caching mechanism.
However in the following case:
fun Activity.b() {
this.textView.setText(“”)
}
We wouldn't know if this function would be invoked on only Activities from our sources or on plain Java Activities also. As such, we don’t use caching there, even if MyActivity instance from the previous example is the receiver.
Kotlin学习中文版:
http://view.xiaomiquan.com/view/59255e4cafd2462904311014
在线kotlin编译网站
https://try.kotlinlang.org/
本文介绍了 Kotlin 在 Android 开发中的应用技巧,包括快速转换 Java 文件为 Kotlin 文件的方法,以及如何利用 Kotlin 的合成属性简化视图操作。同时,文中还探讨了针对不同模块进行差异化打包的策略,并解释了 Kotlin Android Extensions 插件的工作原理。

被折叠的 条评论
为什么被折叠?



