iT邦幫忙

2021 iThome 鐵人賽

DAY 8
0
Mobile Development

Andoroid - Kotlin筆記 (新)系列 第 8

[Day8] Android - Kotlin筆記:JetPack - Core KTX

Core KTX 包含的module有:

androidx.core.animation
androidx.core.content
androidx.core.content.res
androidx.core.database
androidx.core.database.sqlite
androidx.core.graphics
androidx.core.graphics.drawable
androidx.core.location
androidx.core.net
androidx.core.os
androidx.core.text
androidx.core.transition
androidx.core.util
androidx.core.view
androidx.core.widget

這些要一個一個說的話,大概30天都說不完。
所以舉幾個看過比較有趣的。
剩下的大家可以自己試試,細細品味 :)


主要介紹的有:

  • Toast
  • SharedPreferences
  • TextWatcher
  • isVisible

Toast

原本寫法:

Toast.makeText(this,
    R.string.text,
    Toast.LENGTH_SHORT).show()

使用core KTX:

context.toast(R.string.text)

SharedPreferences

inline fun SharedPreferences.edit(
    commit: Boolean = false, 
    action: Editor.() -> Unit
): Unit

儲存資料至的簡潔用法

用法 ex:

spf.edit(commit = true) {
    putString(key, value)
}

對 這寫法真的很舒服 (¯︶¯)
使用前記得先宣告spf:

private val spf by lazy { 
    this.getSharedPreferences(getString(R.string.preference_file_key), Context.MODE_PRIVATE) 
}

TextWatcher

TextView.doAfterTextChanged(crossinline action: (text: Editable?) -> Unit)

只有需要afterTextChanged底下做其他事,
不需要再寫落落ㄉㄥˊ的:

        textView.addTextChangedListener(object : TextWatcher {
            override fun beforeTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun onTextChanged(p0: CharSequence?, p1: Int, p2: Int, p3: Int) {
            }

            override fun afterTextChanged(p0: Editable?) {
                //do something
            }

        })

直接簡化:

textView.doAfterTextChanged {
    //do something
}

isVisible

inline var View.isVisible: Boolean

以前:

textView.visibility = if (isLogin) {
        View.VISIBLE
    } else {
        View.GONE
    }

直接簡化:

textView.isVisible = isLogin

好舒服(¯︶¯)


Core的用法先介紹到這,
如果還有興趣,可以從官網中挖寶喔。


上一篇
[Day7] Android - Kotlin筆記:JetPack - KTX簡介
下一篇
[Day9] Android : Kotlin筆記:JetPack - Fragment KTX
系列文
Andoroid - Kotlin筆記 (新)18
圖片
  直播研討會
圖片
{{ item.channelVendor }} {{ item.webinarstarted }} |
{{ formatDate(item.duration) }}
直播中

尚未有邦友留言

立即登入留言