Android ktx使用

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

ViewModel 使用

// 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 {

        }
    }
}
  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值