栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

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

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

共享一下我的一些 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
}


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)

}

//先共享这么多吧 ,看看有没有人看 没人 就懒得写了
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/838640.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号