lulululbj / androidutilcodektx Goto Github PK
View Code? Open in Web Editor NEWKotlin Android Util Code
Home Page: https://juejin.im/post/5d2adf215188252b13288942
License: Apache License 2.0
Kotlin Android Util Code
Home Page: https://juejin.im/post/5d2adf215188252b13288942
License: Apache License 2.0
这两个应该也是比较常用的,坐等添加~
fun Context.copyToClipboard(label: String, text: String) {
val cm = getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
val clipData = ClipData.newPlainText(label, text)
cm.primaryClip = clipData
}
最后一行报错
String
为例:fun Activity.extraString(key: String, default: String = ""): Lazy<String> = lazy {
intent?.extras?.getString(key) ?: default
}
示例::
private val vinCode: String by extraString(DATA_VIN_CODE)
object Otherwise : BooleanExt<Nothing>()
class TransferData<T>(val data: T) : BooleanExt<T>()
inline fun <T> Boolean.yes(block: () -> T): BooleanExt<T> = when {
this -> TransferData(block.invoke())
else -> Otherwise
}
inline fun <T> BooleanExt<T>.otherwise(block: () -> T): T = when (this) {
is Otherwise -> block.invoke()
is TransferData -> this.data
}
示例:
isNotEmpty().yes {
}.otherwise {
}
这样嵌套用多了代码又丑又难阅读😂
fun View.singleClick(clickEventFun: () -> Unit) {
this.setOnClickListener {
if (Utils.isFastClick()) {
clickEventFun.invoke()
}
}
}
@SuppressLint("CheckResult")
@Deprecated("memory leak")
fun View.singleClick(
intervalDuration: Long = 1,
unit: TimeUnit = TimeUnit.SECONDS,
eventFun: () -> Unit
) {
Observable
.create(object : ObservableOnSubscribe<View> {
lateinit var mEmitter: ObservableEmitter<View>
init {
[email protected] {
mEmitter.onNext(it)
}
}
override fun subscribe(emitter: ObservableEmitter<View>) {
mEmitter = emitter
}
})
.throttleLast(intervalDuration, unit)
.observeOn(AndroidSchedulers.mainThread())
.subscribe {
eventFun()
}
}
activity的管理lifecycle这块为什么没有加进来呢?
我明明是 数据线直接运行的,就是Debug环境,然后你那个LogExt的判断showLog 就是一直返回 false
mException.value = e 在案例中onError方法还是接收不到
java.lang.RuntimeException: Unable to start activity ComponentInfo{luyao.ktx/luyao.util.ktx.mvvm.MvvmActivity}: java.lang.IllegalStateException: You can consumeRestoredStateForKey only after super.onCreate of corresponding component
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2883)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
Caused by: java.lang.IllegalStateException: You can consumeRestoredStateForKey only after super.onCreate of corresponding component
at androidx.savedstate.SavedStateRegistry.consumeRestoredStateForKey(SavedStateRegistry.java:77)
at androidx.lifecycle.AbstractSavedStateViewModelFactory.create(AbstractSavedStateViewModelFactory.java:65)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:177)
at androidx.lifecycle.ViewModelProvider.get(ViewModelProvider.java:145)
at luyao.util.ktx.base.BaseVMActivity.initVM(BaseVMActivity.kt:24)
at luyao.util.ktx.base.BaseVMActivity.onCreate(BaseVMActivity.kt:17)
at android.app.Activity.performCreate(Activity.java:6975)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1214)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2836)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2958)
at android.app.ActivityThread.-wrap11(Unknown Source:0)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1654)
at android.os.Handler.dispatchMessage(Handler.java:105)
at android.os.Looper.loop(Looper.java:164)
at android.app.ActivityThread.main(ActivityThread.java:6653)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:240)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:770)
如题,实际使用中无论是sp还是文件都有时会遇到保存集合数据的时候,但是集合没有本身序列化,每次都又要自己去处理
或者做一个 zip4J 的扩展,https://github.com/srikanth-lingala/zip4j 这个 java 库非常棒,但是他的 api 不太友好,如果用 kt 封装一下就很美好了。
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.