1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
| package com.android.settings;
import android.content.Context; import android.content.res.TypedArray; import android.graphics.Canvas; import android.graphics.Paint; import android.graphics.RectF; import android.util.AttributeSet; import android.util.DisplayMetrics; import android.util.TypedValue; import android.widget.EditText;
import static android.graphics.Paint.ANTI_ALIAS_FLAG;
public class PasswordInputView extends EditText { private int borderColor = 0xFFCCCCCC; private float borderWidth = 5; private float borderRadius = 3; private float dividerWidth = 3; private int passwordLength = 6; private int passwordColor = 0xFFCCCCCC; private float passwordRadius = 3; private Paint paint; private int width, height; public PasswordInputView(Context context, AttributeSet attrs) { super(context, attrs);
DisplayMetrics dm = getResources().getDisplayMetrics(); borderWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, borderWidth, dm); borderRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, borderRadius, dm); passwordLength = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, passwordLength, dm); passwordRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, passwordRadius, dm); TypedArray a = context.getTheme().obtainStyledAttributes(attrs, R.styleable.PasswordInputView, 0, 0); borderColor = a.getColor(R.styleable.PasswordInputView_pivBorderColor, borderColor); borderWidth = a.getDimension(R.styleable.PasswordInputView_pivBorderWidth, borderWidth); borderRadius = a.getDimension(R.styleable.PasswordInputView_pivBorderRadius, borderRadius); passwordLength = a.getInt(R.styleable.PasswordInputView_pivPasswordLength, passwordLength); passwordColor = a.getColor(R.styleable.PasswordInputView_pivPasswordColor, passwordColor); passwordRadius = a.getDimension(R.styleable.PasswordInputView_pivPasswordRadius, passwordRadius); dividerWidth = a.getDimension(R.styleable.PasswordInputView_dividerWidth, dividerWidth); a.recycle();
paint = new Paint(ANTI_ALIAS_FLAG); } @Override protected void onDraw(Canvas canvas) { width = getWidth(); height = getHeight(); drawBorder(canvas); drawDivider(canvas); drawCircle(canvas);
} private void drawBorder(Canvas canvas) { paint.setStyle(Paint.Style.STROKE); paint.setColor(borderColor); paint.setStrokeWidth(borderWidth); RectF rectF = new RectF(0, 0, width, height); canvas.drawRoundRect(rectF, borderRadius, borderRadius, paint); } private void drawDivider(Canvas canvas) { paint.setStyle(Paint.Style.STROKE); paint.setColor(borderColor); paint.setStrokeWidth(dividerWidth); for (int i = 1; i < passwordLength; i++) { int x = i * (width / passwordLength); canvas.drawLine(x, 0, x, height, paint); } } private void drawCircle(Canvas canvas) { paint.setStyle(Paint.Style.FILL); paint.setColor(passwordColor); String content = getText().toString().trim(); for (int i = 0; i < content.length(); i++) { int cx = width / passwordLength / 2 + i * width / passwordLength; int cy = height / 2; canvas.drawCircle(cx, cy, width / passwordLength / 8, paint); } } @Override protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { super.onTextChanged(text, start, lengthBefore, lengthAfter); invalidate(); if (text.length() == passwordLength) { if (onCompleteListener != null) { onCompleteListener.onComplete(text); } } } private OnCompleteListener onCompleteListener;
public void setOnCompleteListener(OnCompleteListener onCompleteListener) { this.onCompleteListener = onCompleteListener; }
public interface OnCompleteListener { void onComplete(CharSequence text); } }
|