轻量 kotlin工具库 & Android工具库
安装
添加仓库
//in build.gradle(Project)
allprojects {
repositories {
maven { url 'https://jitpack.io' }
}
}
// 新版方式 settings.gradle
dependencyResolutionManagement {
repositories {
maven { url 'https://jitpack.io' }
}
}
添加依赖
//in build.gradle(module)
dependencies {
//kotlin utils
implementation "com.github.ve3344.utils:utils-kt:1.5.0"
//android utils
implementation "com.github.ve3344.utils:utils-android:1.5.0"
}
示例
Kotlin 工具
- Boolean 三目运算
//like ?:
val result = (input == "hello").map("yes", "no")
- File 拓展
File("cache").ensureDir()
File("cache").createDirIfNotExists()
File("cache").createFileIfNotExists()
File("cache").length().bytes2kb
- Process 拓展
Runtime.getRuntime().exec("ls").use {
it.waitFor()
}
- 消息摘要工具
ByteArray(5).md5()
ByteArray(5).sha1()
ByteArray(5).sha256()
ByteArray(5).hex()
...
Android 工具
- 快速获取 SystemService
val wm = wifiManager
val bm = bluetoothManager
val am = audioManager
- 快速监听activity等的destroy事件
- 快速toast工具
doOnDestroy {
toast("activity destroy")
}
-
registerReceiver拓展
-
startActivity拓展
registerReceiver(
WifiManager.WIFI_STATE_CHANGED_ACTION,
WifiManager.SCAN_RESULTS_AVAILABLE_ACTION
) {
it ?: return@registerReceiver
val rssi = it.getIntExtra(WifiManager.EXTRA_NEW_RSSI, -1000)
toast("$rssi")
}
startActivity<XXActivity> {
addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK)
withArgs {
"key" to "value"
}
}
- Apk 安装
ApkUtils.installApk(appContext, apkPath)
- Uri和path相互转换
File("test").androidUri().targetPath()
UriUtils.fromAsset("a/b.txt")
- 快速创建 SpannableString
with(findViewById<TextView>(R.id.text)) {
text = SpannableUtils.create {
text("已同意").foregroundColor(Color.DKGRAY)
text("隐私政策").underline().click { toast("隐私政策") }
text("和").absoluteSize(20.sp)
text("用户协议").underline().click { toast("用户协议") }
}
movementMethod = LinkMovementMethod.getInstance()
}
-
点击事件消抖
-
双击退出
-
验证码倒计时
val time = AtomicInteger()
btn.doOnClickAntiShake(1000, {
toast("YOU CLICK TOO FAST")
}) {
toast("REAL CLICK")
textView2.text = "TIME " + time.getAndIncrement()
}
btn.doOnClickMultiple(onClickProgress = {
toast("CLICK AGAIN TO EXIT")
}) {
exitProcess(0)
}
btn.doOnClickCountdown(60, {
textView3.text = if (it > 0) "$it S" else "SEND"
}, {
toast("IN COOLING TIME")
}) {
toast("SEND CODE")
}
btn.configLayoutParams<LinearLayout.LayoutParams> {
width = LinearLayout.LayoutParams.MATCH_PARENT
}
//config LayoutParams