多应用+插件架构,代码干净,二开方便,首家独创一键云编译技术,文档视频完善,免费商用码云13.8K 广告
[Android KTX](https://developer.android.com/kotlin/ktx) Android KTX是一组Kotlin扩展,是Android [Jetpack](https://developer.android.com/jetpack)系列的一部分。它优化了Jetpack和Android平台API以供Kotlin使用。Android KTX的目的是通过利用Kotlin语言功能(如扩展函数/属性,lambdas,命名参数和参数默认值),使Kotlin的Android开发更简洁,愉快和惯用。Android KTX不会为现有的Android API添加任何新功能。 要了解有关Android KTX的更多信息,请参阅我们的[DevBytes视频](https://www.youtube.com/watch?v=r_19VZ0xRO8&feature=youtu.be)。 #### 入门 要开始使用Android KTX,请将以下内容添加到项目的build.gradle文件中: ~~~ repositories { google() } ~~~ Android KTX被组织成模块。每个模块包含一个或多个包。 使用模块时,请在应用程序build.gradle文件中包含每个Android KTX工件的依赖项。请记住将版本附加到工件。例如,如果使用core-ktx模块,则完全形成的依赖关系将如下所示: ~~~ dependencies { implementation 'androidx.core:core-ktx:1.0.0-alpha1' } ~~~ ### 模块 Android KTX由以下Maven工件组成。对于API参考文档,请单击特定包名称并查看扩展功能摘要。 * androidx.core:核心KTX 1.0.0-α1 查看下面的[所有核心包](https://developer.android.com/kotlin/ktx#core-packages)。 * androidx.fragment:片段KTX 1.0.0-α1 [androidx.fragment.app](https://developer.android.com/reference/kotlin/androidx/fragment/app/package-summary#extension-functions-summary) * androidx.palette:调色板KTX 1.0.0-α1 [androidx.palette.graphics](https://developer.android.com/reference/kotlin/androidx/palette/graphics/package-summary#extension-functions-summary) * androidx.sqlite:sqlite的,KTX 1.0.0-α1 [androidx.sqlite.db](https://developer.android.com/reference/kotlin/androidx/sqlite/db/package-summary#extension-functions-summary) * androidx.collection:收集,KTX 1.0.0-α1 [androidx.collection](https://developer.android.com/reference/kotlin/androidx/collection/package-summary#extension-functions-summary) * androidx.lifecycle:生命周期 - 视图模型,KTX 2.0.0-α1 [androidx.lifecycle](https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#extension-functions-summary) * androidx.lifecycle:生命周期reactivestreams,KTX 2.0.0-α1 [androidx.lifecycle](https://developer.android.com/reference/kotlin/androidx/lifecycle/package-summary#extension-functions-summary) * android.arch.navigation:导航,共KTX 1.0.0-alpha01 [androidx.navigation](https://developer.android.com/reference/kotlin/androidx/navigation/package-summary#extension-functions-summary) * android.arch.navigation:导航片段的KTX 1.0.0-alpha01 [androidx.navigation.fragment](https://developer.android.com/reference/kotlin/androidx/navigation/fragment/package-summary#extension-functions-summary) * android.arch.navigation:导航运行,KTX 1.0.0-alpha01 [androidx.navigation](https://developer.android.com/reference/kotlin/androidx/navigation/package-summary#extension-functions-summary) * android.arch.navigation:导航测试,KTX 1.0.0-alpha01 [androidx.navigation.testing](https://developer.android.com/reference/kotlin/androidx/navigation/testing/package-summary#extension-functions-summary) * android.arch.navigation:导航-UI-KTX 1.0.0-alpha01 [androidx.navigation.ui](https://developer.android.com/reference/kotlin/androidx/navigation/ui/package-summary#extension-functions-summary) * android.arch.work:work-runtime-ktx 1.0.0-alpha01 [androidx.work.ktx](https://developer.android.com/reference/kotlin/androidx/work/ktx/package-summary#extension-functions-summary) #### 核心模块包括以下软件包: * [androidx.core.animation](https://developer.android.com/reference/kotlin/androidx/core/animation/package-summary#extension-functions-summary) * [androidx.core.content](https://developer.android.com/reference/kotlin/androidx/core/content/package-summary#extension-functions-summary) * [androidx.core.graphics](https://developer.android.com/reference/kotlin/androidx/core/graphics/package-summary#extension-functions-summary) * [androidx.core.graphics.drawable](https://developer.android.com/reference/kotlin/androidx/core/graphics/drawable/package-summary#extension-functions-summary) * [androidx.core.net](https://developer.android.com/reference/kotlin/androidx/core/net/package-summary#extension-functions-summary) * [androidx.core.os](https://developer.android.com/reference/kotlin/androidx/core/os/package-summary#extension-functions-summary) * [androidx.core.preference](https://developer.android.com/reference/kotlin/androidx/core/preference/package-summary#extension-functions-summary) * [androidx.core.text](https://developer.android.com/reference/kotlin/androidx/core/text/package-summary#extension-functions-summary) * [androidx.core.transition](https://developer.android.com/reference/kotlin/androidx/core/transition/package-summary#extension-functions-summary) * [androidx.core.util](https://developer.android.com/reference/kotlin/androidx/core/util/package-summary#extension-functions-summary) * [androidx.core.view](https://developer.android.com/reference/kotlin/androidx/core/view/package-summary#extension-functions-summary) * [androidx.core.widget](https://developer.android.com/reference/kotlin/androidx/core/widget/package-summary#extension-functions-summary) ### 例子 Android KTX是[Android Jetpack](https://developer.android.com/jetpack/)基础组件。在[Sunflower](https://github.com/googlesamples/android-sunflower)演示应用程序中查看它的使用情况。 以下示例演示了一些Android KTX扩展功能。它们按模块(工件)名称分组。有关扩展功能的完整列表,请参阅完整的包参考文档。 #### androidx.core:核心KTX ① **KOTLIN** ~~~ sharedPreferences.edit() .putBoolean("key", value) .apply() ~~~ **KOTLIN + ANDROID KTX** ~~~ sharedPreferences.edit { putBoolean("key", value) } ~~~ ② **KOTLIN** ~~~ view.viewTreeObserver.addOnPreDrawListener( object : ViewTreeObserver.OnPreDrawListener { override fun onPreDraw(): Boolean { viewTreeObserver.removeOnPreDrawListener(this) actionToBeTriggered() return true } } ) ~~~ **KOTLIN + ANDROID KTX** ~~~ view.doOnPreDraw { actionToBeTriggered() } ~~~ #### androidx.sqlite:sqlite-ktx **KOTLIN** ~~~ db.beginTransaction() try { // insert data db.setTransactionSuccessful() } finally { db.endTransaction() } ~~~ **KOTLIN + ANDROID KTX** ~~~ db.transaction { // insert data } ~~~ #### androidx.fragment:fragment-ktx **KOTLIN** ~~~ supportFragmentManager .beginTransaction() .replace(R.id.my_fragment_container, myFragment, FRAGMENT_TAG) .commitAllowingStateLoss() ~~~ **KOTLIN + ANDROID KTX** ~~~ supportFragmentManager.transaction(allowStateLoss = true) { replace(R.id.my_fragment_container, myFragment, FRAGMENT_TAG) } ~~~ #### 反馈 Android KTX目前在Alpha。我们将继续在Jetpack中添加新的Kotlin扩展,我们欢迎您的反馈。如果您想报告问题或建议功能,请在Android KTX 问题跟踪器上提交新问题。