共享一下我的一些 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”.show
fun 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)

}

//先共享这么多吧 ,看看有没有人看 没人 就懒得写了


// 如果对你有帮助 请点留下足迹 让我知道 谢谢

第二弹 来了
https://blog.csdn.net/u013958112/article/details/124451710

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

疯狂的小彭彭

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值