Android开发使用kotline语言学习笔记

快捷键:

 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.csdn.net/myliuyx/article/details/52223066 productflavor

TODO Android flavor这里也没看懂

Android Flavors

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 方法在类继承间的使用

Under the hood

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 dont 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/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值