//监听edittext 输入
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?) {
}
}
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)
}
//启动activity 在fragment中调用start方法
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
}
}
//启动activity 在activity中调用start方法 单参数
fun Activity.start(act: Class<*>, key: String = "") {
val intent = Intent(this, act)
if (key.isNotEmpty()) {
intent.putExtra("data", key)
}
this.startActivity(intent)
}
//启动activity 在activity中调用start方法 大量数据 时 文件存储 传key
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<String>) {
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<String>
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<String>) {
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 //中划线
}
/**
* TextView 加粗
*/
fun TextView.jiacu() {
this.paint.isFakeBoldText = true
}
//下划线
fun TextView.xhx() {
this.paint.flags =
Paint.UNDERLINE_TEXT_FLAG
}
fun ListToStr(descImg: MutableList<String>): 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 < 6) {
"请输入6位密码".show
return false
}
if (this.text.toString() != ed.text.toString()) {
"两次密码输入不一致".show
return false
}
return true
}
// 验证 验证码
val EditText.isCode: Boolean
get() {
if (this.text.toString().length != 4) {
"请输入4位验证码".show()
return false
}
return true
}
val String.isCarNum: Boolean
get() {
val regEx1 = "[\\u4e00-\\u9fa5][A-Z][A-Z_0-9]{5}"
val p: Pattern = Pattern.compile(regEx1)
val m: Matcher = p.matcher(this)
if (!m.matches()) {
"请输入正确的车牌号".show()
return false
}
return true
}
val Int.toPx: Int
get() {
val scale: Float = BaseApplication.getInstance().resources.displayMetrics.density
return (this * scale + 0.5f).toInt()
}
/**
* 设置状态栏文字 黑色 或者白色 true 为白色
*/
var Activity.topTxtWhite: Boolean
get() = true
set(boo) {
val decorView: View = this.window.decorView
if (Build.VERSION.SDK_INT >= 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()
}
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
}
共享一下我的一些 kotlin的扩展方法2
于 2022-04-27 15:10:51 首次发布
本文介绍了如何监听EditText输入并处理计数,ViewPager的滚动控制,以及打电话、启动Activity和复制粘贴功能。还展示了如何使用CountDownTimer实现验证码倒计时,以及密码验证和格式检查功能。
266

被折叠的 条评论
为什么被折叠?



