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

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

//用SmartRefreshLayout 的需要结束动画 这个扩展方法 可以少些一行代码//以下代码  都直接放在kt文件中 不要放在class 里面  切忌fun SmartRefreshLayout.Fin() {    this.finishLoadMore()    this.finishRefresh()}// 用法 “aaaa”.log  变量也行fun String.log() {    log.e(this)}private var toast: Toast? = null// 用法 “aaa”.showfun show(info: Any?) {    if (info.toString().isEmpty() || info.toString().length > 100 || info.toString()     .contains("code")    ) { return    }    if (toast == null) { toast = Toast.makeText(BaseApplication.getInstance(), null, Toast.LENGTH_SHORT)    }    try { toast!!.setText(info.toString() + "") toast!!.show()    } catch (c: Exception) { LogUtils.e(c.message)    }}//用法 edittext.IsIdCard() 判断输入框中是不是身份证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}//判断string 是不是身份证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}/** ViewPager 无限滚动 调用jop 方法 即可 * @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())    }}// 对象 转String   var u=usermode()    u.MtoStrin()  应为是any 所以 所有的类 都可用fun Any.MtoString(): String {    return GsonBuilder().disableHtmlEscaping().create().toJson(this)}//先共享这么多吧 ,看看有没有人看 没人 就懒得写了