31DaysOfKotlin-series 6

  • Post author:
  • Post category:其他



Google推送的31天快速了解Kotlin,这篇就是最后一个了。但我还会继续从平时的学习中分享一些有用的学习经历

  1. Android KTX提供了bundle creator,可以让我们在创建bundle的时候更加简洁,不用再调用’putString’, ‘putInt’等方法,调用一次就返回一个新bundle
   val bundle = bundleOf(
       	"KEY_INT" to 1, 
       	"KEY_LONG" to 2L,
       	"KEY_BOOLEAN" to true,
       	"KEY_NULL" to null,
       	"KEY_ARRAY" to arrayOf(1, 2)
   	)


android/android-ktxandroid-ktx – A set of Kotlin extensions for Android app development.github.com

  1. 当我们最后一个参数是回调的时候,你可以通过Lambda来cleanup callbacks,Callable以及Runnable。比如,Android KTX提供了‘postDelayed’ with Lambda。

    // Android KTX API
    fun Handler.postDelayed(delay: Int /* ... */, action: ()-> Unit)
    
    // Call it like this - no need for a Runnable in your code
    handler.postDelayed(50) {
    	// pass a lambda to postDelayed
    }
    


    android/android-ktxandroid-ktx – A set of Kotlin extensions for Android app development.github.com

  2. Kotlin KTX给一些最常用的spans添加了extension,这样可以让其相关的API更加使用起来更简单。

    var string = buildSpannedString {
        append("no styling text")
        bold {
            append("bold")
            italic {
                append("bold and italic")
            }
        }
        inSpans(RelativeSizeSpan(2f), QuoteSpan()) {
            append("double sized quote text")
        }
    }
    


    android/android-ktxandroid-ktx – A set of Kotlin extensions for Android app development.github.com

  3. Kotlin提供了

    Parcelize

    可以让你实现

    Parcelable

    的时候更加的简洁

    @Parcelize
    data class User(val name: String, val occupation: Work): Parcelable
    
    // build.gradle
    androidExtensions {
        // Enable experimental Kotlin features in gradle to enable Parcelize
        experimental = true
    }
    


    Kotlin/KEEPKotlin Evolution and Enhancement Process. Contribute to KEEP development by creating an…github.com

  4. 通过使用default的参数列表来扩展现有函数API。Android KTX可以让你设置任意的一个参数。

    view.updatePadding(left = newPadding)
    view.updatePadding(top = newPadding)
    view.updatePadding(right = newPadding)
    view.updatePadding(bottom = newPadding)
    
    view.updatePadding(top = newPadding, bottom = newPadding)
    


    android/android-ktxandroid-ktx – A set of Kotlin extensions for Android app development.github.com

  5. ‘let’, ‘apply’, ‘with’和‘run’都有一个receiver(this), 可能有一个argument(it)以及可能有一个返回值,下面来看一下它们的区别:

    val string = "a"
    val result = string.run {
        // this = "a"
        // it = not available
        1 // Block return value
        // result = 1
    }
    
    val string = "a"
    val result = string.let {
        // this = this@MyClass
        // it = "a"
        2 // Block return value
        // result = 2
    }
    
    val string = "a"
    val result = with(string) {
        // this = "a"
        // it = not available
        3 // Block return value
        // result = 3
    }
    
    val string = "a"
    val result = string.apply {
        // this = "a"
        // it = not available
        4 // Block return value unused
        // result = "a"
    }
    


    Read More in Kotlin Core Funcitons



版权声明:本文为swust_chenpeng原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。