Kotlin 扩展函数

创建一个CommonExt kotlin 文件,直接复制以下代码就可以直接使用了

刚开始学些kotlin

/**
 * 扩展方法管理
 * shige chen on 2020/5/13
 */

/**扩展String函数判断字符串不为空且内容未null*/
fun String.isNotNull(): Boolean {
  return null != this && isNotEmpty() && !"null".equals(this, true)
}

/**复制字符串*/
inline fun String.copy(mContext: Context, block: () -> Unit) {
  //获取剪贴版管理器
  val mClipboardManager: ClipboardManager = mContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager
  //创建普通字符型ClipData
  val mClipData = ClipData.newPlainText("Label", this)
  //将ClipData 内容放到系统剪贴版里
  mClipboardManager.primaryClip = mClipData
  block()
}

/**dp转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  Context.onStart() {
  startActivity(Intent(this, T::class.java))
}

/**启动Activity*/
inline fun  Context.onStart(mKey: String, mBundle: Bundle?) {
  val mIntent = Intent(this, T::class.java)
  mIntent.putExtra(mKey, mBundle)
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Context.onStart(vararg pair: Pair?) {
  val mIntent = Intent(this, T::class.java)
  pair?.let {
    pair.forEach {
      mIntent.putExtra(it!!.first, it!!.second)
    }
  }
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Activity.onStart() {
  startActivity(Intent(this, T::class.java))
}

/**启动Activity*/
inline fun  Activity.onStart(mKey: String, mBundle: Bundle?) {
  val mIntent = Intent(this, T::class.java)
  mIntent.putExtra(mKey, mBundle)
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Activity.onStart(vararg pair: Pair?) {
  val mIntent = Intent(this, T::class.java)
  pair?.let {
    pair.forEach {
      mIntent.putExtra(it!!.first, it!!.second)
    }
  }
  startActivity(mIntent)
}

/**启动Activity*/
inline fun  Activity.onStartResult(requestCode: Int) {
  startActivityForResult(Intent(this, T::class.java), requestCode, null)
}

/**启动Activity*/
inline fun  Activity.onStartResult(requestCode: Int, mBundle: Bundle?) {
  startActivityForResult(Intent(this, T::class.java), requestCode, mBundle)
}

/**启动Activity*/
inline fun  Activity.onStartResult(requestCode: Int, vararg pair: Pair) {
  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  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  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  Context.newInstanceFragment(vararg pair: Pair): 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  LiveData.observe(owner: LifecycleOwner, crossinline block: (T) -> Unit): Observer {
    val wrappedObserver = Observer {
        it?.let { block.invoke(it) }
    }
    observe(owner, wrappedObserver)
    return wrappedObserver
}

inline fun  MutableLiveData.observe(owner: LifecycleOwner, crossinline block: (T) -> Unit): Observer {
    val wrappedObserver = Observer {
        it?.let { block.invoke(it) }
    }
    observe(owner, wrappedObserver)
    return wrappedObserver
}

inline fun  Observable.onEvent(owner: LifecycleOwner, crossinline block: (T) -> Unit): Observer {
    val wrappedObserver = Observer {
        it?.let { block.invoke(it) }
    }
    observe(owner, wrappedObserver)
    return wrappedObserver
}

你可能感兴趣的:(Kotlin 扩展函数)