1.自定义view
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.view.View
import java.lang.StringBuilder
/**
* @author wellJan
* 自定义密码框
*/
class TestView : View {
constructor(context: Context?) :super(context,null,0)
constructor(context: Context?, attrs: AttributeSet?) :super(context, attrs,0)
constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) :super(
context,
attrs,
defStyleAttr
)
private var paint = Paint()
private var pointPaint = Paint()
//单个密码框的宽度
private var itemW =0
//item的间隔
private var itemPadding =20;
private var size =6
init {
paint.color = (Color.BLACK)
paint.isAntiAlias =true
paint.style = Paint.Style.STROKE
setBackgroundColor(Color.WHITE)
pointPaint.color = (Color.BLACK)
pointPaint.isAntiAlias =true
pointPaint.style = Paint.Style.FILL
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
//先绘制size个密码框框
itemW = (measuredWidth-paddingLeft-paddingRight-(size-1)*itemPadding)/size;
canvas!!.translate(paddingLeft.toFloat(),0f)
canvas.save()
var rect = Rect(0,0,itemW,measuredHeight)
for (iin 0 until size){
canvas!!.drawRect(rect,paint)
if (i
//画实心圆
canvas.drawCircle(itemW/2.toFloat(),measuredHeight/2.toFloat(),itemW/size.toFloat(),pointPaint)
}
canvas.translate(itemPadding.toFloat()+itemW,0f)
}
canvas!!.restore()
}
var stringBuilder = StringBuilder()
public fun addNumber(number:String){
if (stringBuilder.length ==size)
return
stringBuilder.append(number)
invalidate()
}
public fun delNumber(){
if (stringBuilder.isEmpty())
return
stringBuilder.deleteAt(stringBuilder.length-1)
invalidate()
}
public fun getNumber():String{
return stringBuilder.toString()
}
}