转载请标明出处:http://blog.csdn.net/zhaoyanjun6/article/details/125547535
本文出自【赵彦军的博客】
文章目录
core-ktx
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.updateLayoutParams<FrameLayout.LayoutParams> {
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
viewGroup.allViews
viewGroup.children
TextView
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 = getSystemService<WindowManager>()
collection-ktx
依赖:
implementation "androidx.collection:collection-ktx:1.2.0"
版本号查询:https://mvnrepository.com/artifact/androidx.collection/collection-ktx
map
val map = arrayMapOf<String,String>()
map.put("key","")
set
val map = arraySetOf<String>()
map.add("123")
activity-ktx
依赖:
implementation 'androidx.activity:activity-ktx:1.5.0'
版本号查询:https://mvnrepository.com/artifact/androidx.activity/activity-ktx
ViewModel 使用
val viewModel by viewModels<MyViewModel>()
fragment-ktx
依赖:
implementation 'androidx.fragment:fragment-ktx:1.4.1'
版本号查询:https://mvnrepository.com/artifact/androidx.fragment/fragment-ktxViewModel 使用
// Get a reference to the ViewModel scoped to this Fragment
val viewModel by viewModels<MyViewModel>()
// Get a reference to the ViewModel scoped to its Activity
val viewModel by activityViewModels<MyViewModel>()
lifecycle-ktx
依赖:
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.5.0'
版本号查询:https://mvnrepository.com/artifact/androidx.lifecycle/lifecycle-runtime-ktx
lifecycleScope
Activity
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 {
}
}
}
————————————————
版权声明:本文为CSDN博主「赵彦军」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/zhaoyanjun6/article/details/125547535