栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Android ktx使用

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Android ktx使用

转载请标明出处: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

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 {
    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()
collection-ktx

依赖:

implementation "androidx.collection:collection-ktx:1.2.0"

版本号查询:https://mvnrepository.com/artifact/androidx.collection/collection-ktx

map
val map = arrayMapOf()
map.put("key","")
set
val map = arraySetOf()
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()
fragment-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 viewModels()

// Get a reference to the ViewModel scoped to its Activity
val viewModel by activityViewModels()
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 {

        }
    }
}
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/1002774.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号