創(chuàng)建一個CommonExt kotlin 文件,直接復(fù)制以下代碼就可以直接使用了
剛開始學(xué)些kotlin
/**
* 擴展方法管理
* shige chen on 2020/5/13
*/
/**擴展String函數(shù)判斷字符串不為空且內(nèi)容未null*/
fun String.isNotNull(): Boolean {
return null != this && isNotEmpty() && !"null".equals(this, true)
}
/**復(fù)制字符串*/
inline fun String.copy(mContext: Context, block: () -> Unit) {
//獲取剪貼版管理器
val mClipboardManager: ClipboardManager = mContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
//創(chuàng)建普通字符型ClipData
val mClipData = ClipData.newPlainText("Label", this)
//將ClipData 內(nèi)容放到系統(tǒng)剪貼版里
mClipboardManager.primaryClip = mClipData
block()
}
/**dp轉(zhuǎn)px*/
fun Int.px(): Int {
return (Resources.getSystem().displayMetrics.density * this + 0.5f).roundToInt()
}
/**防止快速點擊*/
inline fun View.onClick(crossinline block: (View) -> Unit) {
setOnClickListener {
AppUtil.isFastDoubleClick().on {
block(it)
}
}
}
/**啟動Activity*/
inline fun <reified T : Activity> Context.onStart() {
startActivity(Intent(this, T::class.java))
}
/**啟動Activity*/
inline fun <reified T : Activity> Context.onStart(mKey: String, mBundle: Bundle?) {
val mIntent = Intent(this, T::class.java)
mIntent.putExtra(mKey, mBundle)
startActivity(mIntent)
}
/**啟動Activity*/
inline fun <reified T : Activity> Context.onStart(vararg pair: Pair<String, String>?) {
val mIntent = Intent(this, T::class.java)
pair?.let {
pair.forEach {
mIntent.putExtra(it!!.first, it!!.second)
}
}
startActivity(mIntent)
}
/**啟動Activity*/
inline fun <reified T : Activity> Activity.onStart() {
startActivity(Intent(this, T::class.java))
}
/**啟動Activity*/
inline fun <reified T : Activity> Activity.onStart(mKey: String, mBundle: Bundle?) {
val mIntent = Intent(this, T::class.java)
mIntent.putExtra(mKey, mBundle)
startActivity(mIntent)
}
/**啟動Activity*/
inline fun <reified T : Activity> Activity.onStart(vararg pair: Pair<String, String>?) {
val mIntent = Intent(this, T::class.java)
pair?.let {
pair.forEach {
mIntent.putExtra(it!!.first, it!!.second)
}
}
startActivity(mIntent)
}
/**啟動Activity*/
inline fun <reified T : Activity> Activity.onStartResult(requestCode: Int) {
startActivityForResult(Intent(this, T::class.java), requestCode, null)
}
/**啟動Activity*/
inline fun <reified T : Activity> Activity.onStartResult(requestCode: Int, mBundle: Bundle?) {
startActivityForResult(Intent(this, T::class.java), requestCode, mBundle)
}
/**啟動Activity*/
inline fun <reified T : Activity> Activity.onStartResult(requestCode: Int, vararg pair: Pair<String, String>) {
val mIntent = Intent(this, T::class.java)
val mBundle = Bundle()
pair?.let {
pair.forEach {
mBundle.putString(it!!.first, it!!.second)
}
}
startActivityForResult(mIntent, requestCode, mBundle)
}
/**實例化 Fragment*/
inline fun <reified T : Fragment> Context.newInstanceFragment(): T {
val args = Bundle()
val className = T::class.java.name;
val clazz = FragmentFactory.loadFragmentClass(
classLoader, className)
val f = clazz.getConstructor().newInstance()
if (args != null) {
args.classLoader = f.javaClass.classLoader
f.arguments = args
}
return f as T
}
/**實例化 Fragment*/
inline fun <reified T : Fragment> Context.newInstanceFragment(args: Bundle?): T {
val className = T::class.java.name;
val clazz = FragmentFactory.loadFragmentClass(
classLoader, className)
val f = clazz.getConstructor().newInstance()
if (args != null) {
args.classLoader = f.javaClass.classLoader
f.arguments = args
}
return f as T
}
/**實例化 Fragment*/
inline fun <reified T : Fragment> Context.newInstanceFragment(vararg pair: Pair<String, String>): T {
val args = Bundle()
pair?.let {
for (arg in pair) {
args.putString(arg.first, arg.second)
}
}
val className = T::class.java.name;
val clazz = FragmentFactory.loadFragmentClass(
classLoader, className)
val f = clazz.getConstructor().newInstance()
if (args != null) {
args.classLoader = f.javaClass.classLoader
f.arguments = args
}
return f as T
}
inline fun <T : Any> LiveData<T>.observe(owner: LifecycleOwner, crossinline block: (T) -> Unit): Observer<T> {
val wrappedObserver = Observer<T> {
it?.let { block.invoke(it) }
}
observe(owner, wrappedObserver)
return wrappedObserver
}
inline fun <T : Any> MutableLiveData<T>.observe(owner: LifecycleOwner, crossinline block: (T) -> Unit): Observer<T> {
val wrappedObserver = Observer<T> {
it?.let { block.invoke(it) }
}
observe(owner, wrappedObserver)
return wrappedObserver
}
inline fun <T : Any> Observable<T>.onEvent(owner: LifecycleOwner, crossinline block: (T) -> Unit): Observer<T> {
val wrappedObserver = Observer<T> {
it?.let { block.invoke(it) }
}
observe(owner, wrappedObserver)
return wrappedObserver
}
最后編輯于 :
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者