> 文档中心 > 共享一下我的一些 kotlin的扩展方法2

共享一下我的一些 kotlin的扩展方法2

open class TextWatcherLP : TextWatcher {    override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {    }    override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {    }    override fun afterTextChanged(s: Editable?) {    }}fun SmartRefreshLayout.Fin() {    this.finishLoadMore()    this.finishRefresh()}fun EditText.IsIdCard(msg: String? = "", msg2: String? = ""): Boolean {    if (this.text.toString().isEmpty()) { if (msg == null || msg.isEmpty()) {     "请输入身份证号".show } else {     msg.show } return false    }    val r1 = Regex("^[1-9][0-9]{5}(18|19|20)[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}([0-9]|(X|x))")    if (!r1.matches(this.text.toString())) { if (msg2 == null || msg2.isEmpty()) {     "请输入正确身份证号".show } else {     msg2.show } return false    }    return true}fun String.IsIdCard(msg: String? = "", msg2: String? = ""): Boolean {    if (this.isEmpty()) { if (msg == null || msg.isEmpty()) {     "请输入身份证号".show } else {     msg.show } return false    }    val r1 = Regex("^[1-9][0-9]{5}(18|19|20)[0-9]{2}((0[1-9])|(10|11|12))(([0-2][1-9])|10|20|30|31)[0-9]{3}([0-9]|(X|x))")    if (!r1.matches(this)) { if (msg2 == null || msg2.isEmpty()) {     "请输入正确身份证号".show } else {     msg2.show } return false    }    return true}/ * 计算距离 */val handlers: Handler = Handler(Looper.getMainLooper()) { msg ->    if (msg.what == 1993) { val vp = msg.obj as ViewPager vp.adapter?.count?.let { it ->     if (vp.currentItem + 1 == it) {  vp.currentItem = 0     } else {  vp.currentItem++     }     vp.jop(msg.arg1) }    }    false}/ * @param s 间隔时长 * */fun ViewPager.jop(s: Int) {    this.adapter?.count?.let { val msg = Message() msg.obj = this msg.what = 1993 msg.arg1 = s handlers.sendMessageDelayed(msg, s.toLong())    }}fun callPhone(context: Context, phoneNum: String) {    val intent = Intent(Intent.ACTION_DIAL)    val data = Uri.parse("tel:$phoneNum")    intent.data = data    context.startActivity(intent)}fun Fragment.start(act: Class, key: String = "") {    val intent = Intent(this.activity, act)    if (key.isNotEmpty()) { intent.putExtra("data", key)    }    this.startActivity(intent)}fun copy(mContext: Context, copyStr: String?): Boolean {    return try { //获取剪贴板管理器 val cm = mContext.getSystemService(Context.CLIPBOARD_SERVICE) as ClipboardManager // 创建普通字符型ClipData val mClipData = ClipData.newPlainText("Label", copyStr) // 将ClipData内容放到系统剪贴板里。 cm.setPrimaryClip(mClipData) true    } catch (e: java.lang.Exception) { false    }}fun Activity.start(act: Class, key: String = "") {    val intent = Intent(this, act)    if (key.isNotEmpty()) { intent.putExtra("data", key)    }    this.startActivity(intent)}fun Activity.start(act: Class, key: String = "", dataId: String = "") {    val intent = Intent(this, act)    if (key.isNotEmpty()) { intent.putExtra("data", key)    }    if (dataId.isNotEmpty()) { intent.putExtra("dataId", dataId)    }    this.startActivity(intent)}fun Activity.start(act: Class, key: ArrayList) {    val intent = Intent(this, act)    if (key.isNotEmpty()) { intent.putExtra("data", key)    }    this.startActivity(intent)}val Activity.data: String    get() { if (this.intent.hasExtra("data") && intent.getStringExtra("data") != null) {     return this.intent.getStringExtra("data")!! } return ""    }val Activity.dataId: String    get() { if (this.intent.hasExtra("dataId") && intent.getStringExtra("dataId") != null) {     return this.intent.getStringExtra("dataId")!! } return ""    }val Activity.arr: ArrayList    get() { if (this.intent.hasExtra("data") && intent.getStringArrayListExtra("data") != null) {     return this.intent.getStringArrayListExtra("data")!! } return arrayListOf()    }fun Activity.startResult(act: Class, requestCode: Int, key: String = "") {    val intent = Intent(this, act)    if (key.isNotEmpty()) { intent.putExtra("data", key)    }    this.startActivityForResult(intent, requestCode)}fun Activity.startResult(act: Class, requestCode: Int, key: ArrayList) {    val intent = Intent(this, act)    if (key.isNotEmpty()) { intent.putExtra("data", key)    }    this.startActivityForResult(intent, requestCode)}fun TextView.zhx() {    this.paint.flags = Paint.STRIKE_THRU_TEXT_FLAG or Paint.ANTI_ALIAS_FLAG     //中划线}/ * 加粗 */fun TextView.jiacu() {    this.paint.isFakeBoldText = true}fun TextView.xhx() {    this.paint.flags = Paint.UNDERLINE_TEXT_FLAG    //下划线}fun ListToStr(descImg: MutableList): String {    var s = ""    for (a in descImg) { if (s != "") {     s += "," } s += a    }    return s}/ * 显示/隐藏  占位置 */fun View.VIV(tv: Boolean) {    visibility = if (tv) View.VISIBLE else View.INVISIBLE}/ * 显示/消失 */fun View.VG(vg: Boolean) {    visibility = if (vg) View.VISIBLE else View.GONE}val TextView.send: Unit    get() { val tv = this val count = object : CountDownTimer(60 * 1000, 1000) {     override fun onTick(millisUntilFinished: Long) {  tv.text = (millisUntilFinished / 1000).toString()  tv.isClickable = false     }     override fun onFinish() {  tv.text = "获取验证码"  tv.isClickable = true     } } count.start()    }fun EditText.passWordeq(ed: EditText): Boolean {    if (this.text.toString().length = Build.VERSION_CODES.M) {     decorView.systemUiVisibility =  (View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE // 白色文字   or if (boo) View.SYSTEM_UI_FLAG_VISIBLE else View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR) } else {     decorView.systemUiVisibility =  View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN or View.SYSTEM_UI_FLAG_LAYOUT_STABLE }    }val Int.dp: Int    get() { val scale: Float = BaseApplication.getInstance().resources.displayMetrics.density return (this * scale + 0.5f).toInt()    }val String.log: Unit    get() { LogUtils.e(this)    }var toasts: Toast? = nullval String.show: Unit    get() { if (this.isEmpty() || this.length > 100) {     return } if (toasts == null) {     toasts = Toast.makeText(BaseApplication.getInstance(), null, Toast.LENGTH_SHORT) } try {     toasts!!.setText(this)     toasts!!.show() } catch (c: Exception) {     LogUtils.e(c.message) }    }fun String.show() {    if (this.isEmpty() || this.length > 100) { return    }    if (toasts == null) { toasts = Toast.makeText(BaseApplication.getInstance(), null, Toast.LENGTH_SHORT)    }    try { toasts!!.setText(this) toasts!!.show()    } catch (c: Exception) { LogUtils.e(c.message)    }}fun String.showLong() {    if (this.isEmpty() || this.length > 100) { return    }    if (toasts == null) { toasts = Toast.makeText(BaseApplication.getInstance(), null, Toast.LENGTH_LONG)    }    try { toasts!!.setText(this) toasts!!.show()    } catch (c: Exception) { LogUtils.e(c.message)    }}private val CHINA_PATTERN =    Pattern.compile("^((13[0-9])|(14[0,1,4-9])|(15[0-3,5-9])|(16[2,5,6,7])|(17[0-8])|(18[0-9])|(19[0-3,5-9]))\\d{8}$")fun String.isPhone(): Boolean {    val m: Matcher = CHINA_PATTERN.matcher(this)    if (!m.matches()) { "请输入正确的手机号".show return false    }    return m.matches()}fun String.toIntForhttp(): String {    var a = this    a = "$a:int"    return a}fun String.toBol(): String {    var a = this    a = "$a:bol"    return a}fun isServiceRunning(context: Context, serviceName: String): Boolean {    val am = context.getSystemService(Context.ACTIVITY_SERVICE) as ActivityManager    val runningServiceInfos = am.getRunningServices(200)    if (runningServiceInfos.size <= 0) { LogUtils.e("11111111111runningServiceInfos.size <= 0") return false    }    for (serviceInfo in runningServiceInfos) { LogUtils.e("11111111111" + serviceInfo.service.className) if (serviceInfo.service.className == serviceName) {     return true }    }    return false}