首先声明,此文借鉴于https://blog.csdn.net/oLimxing/article/details/89145972 我只是做了些修改,感谢前人栽的树,感谢巨人的肩膀!
效果图(视觉效果很差。。)
修改的源码如下
拿到原作者的源码之后,为了满足项目需求,到处乱改了一通,具体改了哪里,具体什么原理都没有搞清楚,就只会做搬运工
package me.leefeng.libverify
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.drawable.ColorDrawable
import android.text.Editable
import android.text.InputFilter
import android.text.InputType
import android.text.TextWatcher
import android.util.AttributeSet
import android.util.Log
import android.util.TypedValue
import android.view.Gravity
import android.view.KeyEvent
import android.view.View
import android.view.ViewGroup
import android.view.inputmethod.InputMethodManager
import android.widget.EditText
import android.widget.TextView
/**
*
* Created by lilifeng on 2019/4/8
*
* 自定义View - 手机验证码输入框
*
* 经过修改
*/
class VerificationView @JvmOverloads constructor(
context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0
) : ViewGroup(context, attrs, defStyleAttr), TextWatcher, View.OnKeyListener {
override fun onKey(v: View?, keyCode: Int, event: KeyEvent?): Boolean {
if (keyCode == KeyEvent.KEYCODE_DEL && event?.action == KeyEvent.ACTION_UP) {
if (v?.tag == (etTextCount - 1)) {
if (lastEtisEmpty) {
backFocus()
}
lastEtisEmpty = true
} else {
backFocus()
}
}
return false
}
override fun afterTextChanged(s: Editable?) {
synchronized(this) {
focus()
}
}
private var lastChar:String = ""
private var lastEtisEmpty = true
override fun beforeTextChanged(s: CharSequence?, start: Int, count: Int, after: Int) {
val lastETC = getChildAt(etTextCount - 1) as EditText
lastEtisEmpty = lastETC.text.isEmpty()
if (!lastEtisEmpty){
lastChar = s.toString()
}else{
lastChar = ""
}
}
override fun onTextChanged(s: CharSequence?, start: Int, before: Int, count: Int) {
if (s!!.length > 1) {
clear()