文章目录转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/125547535
本文出自【赵彦军的博客】
- core-ktx
- View
- ViewGroup
- TextView
- SharedPreferences
- URI
- ObjectAnimator
- Context
- collection-ktx
- map
- set
- activity-ktx
- ViewModel 使用
- fragment-ktx
- ViewModel 使用
- lifecycle-ktx
- lifecycleScope
Android KTX 是包含在 Android Jetpack 及其他 Android 库中的一组 Kotlin 扩展程序。KTX 扩展程序可以为 Jetpack、Android 平台及其他 API 提供简洁的惯用 Kotlin 代码。
简单来说 , ktx 就是:不增加任何新功能,只是对原有功能进行扩展,来达到简化使用的目的。
官方地址:https://developer.android.google.cn/kotlin/ktx
maven版本号:https://mvnrepository.com/artifact/androidx.core/core-ktx
添加依赖:
implementation 'androidx.core:core-ktx:1.8.0'View
view.isGone = true view.isVisible = false view.updateLayoutParamsViewGroup{ width = 100 } view.marginTop view.marginBottom view.marginStart view.marginEnd view.doOnAttach { } view.doOnDetach { } view.setPadding(100) view.postDelayed(100){ } val bitmap = view.drawToBitmap(Bitmap.Config.ARGB_8888) view.allViews
viewGroup.allViews viewGroup.childrenTextView
text.doBeforeTextChanged { text, start, count, after -> }
text.doOnTextChanged { text, start, before, count -> }
text.doAfterTextChanged { }
text.addTextChangedListener { }
SharedPreferences
val sp = getSharedPreferences("file_name", MODE_PRIVATE)
sp.edit(commit = false) {
putString("key", "2")
}
URI
val uri = "123".toUri() uri.toFile()ObjectAnimator
val objectAnimator = ObjectAnimator.ofFloat(text, "translationX", -200f, 800f)
objectAnimator.doOnStart {}
objectAnimator.doOnPause {}
objectAnimator.doOnEnd {}
objectAnimator.doOnRepeat {}
objectAnimator.doOnResume {}
objectAnimator.duration = 3000
objectAnimator.startDelay = 100
objectAnimator.start()
Context
//常规 val windowManager = getSystemService(Context.WINDOW_SERVICE) as WindowManager //ktx val windowManagerKT = getSystemServicecollection-ktx()
依赖:
implementation "androidx.collection:collection-ktx:1.2.0"
版本号查询:https://mvnrepository.com/artifact/androidx.collection/collection-ktx
mapval map = arrayMapOfset() map.put("key","")
val map = arraySetOfactivity-ktx() map.add("123")
依赖:
implementation 'androidx.activity:activity-ktx:1.5.0'
版本号查询:https://mvnrepository.com/artifact/androidx.activity/activity-ktx
ViewModel 使用val viewModel by viewModelsfragment-ktx()
依赖:
implementation 'androidx.fragment:fragment-ktx:1.4.1'
版本号查询:https://mvnrepository.com/artifact/androidx.fragment/fragment-ktx
ViewModel 使用// Get a reference to the ViewModel scoped to this Fragment val viewModel by viewModelslifecycle-ktx() // Get a reference to the ViewModel scoped to its Activity val viewModel by activityViewModels ()
依赖:
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
版本号查询:https://mvnrepository.com/artifact/androidx.lifecycle/lifecycle-runtime-ktx
lifecycleScopeActivity
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch { }
}
}
Fragment
class MyF : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
lifecycleScope.launch { }
}
}
自定义 View
class MyView(context: Context?, attrs: AttributeSet?) : View(context, attrs) {
init {
findViewTreeLifecycleOwner()?.lifecycleScope?.launch {
}
}
}



