|
| 1 | +package com.allen.androidcustomview.widget; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | +import android.content.res.TypedArray; |
| 5 | +import android.graphics.Canvas; |
| 6 | +import android.graphics.Color; |
| 7 | +import android.graphics.Paint; |
| 8 | +import android.graphics.RectF; |
| 9 | +import android.text.InputFilter; |
| 10 | +import android.text.TextUtils; |
| 11 | +import android.util.AttributeSet; |
| 12 | +import android.widget.EditText; |
| 13 | + |
| 14 | +import com.allen.androidcustomview.R; |
| 15 | + |
| 16 | + |
| 17 | +import static android.graphics.Paint.ANTI_ALIAS_FLAG; |
| 18 | + |
| 19 | +/** |
| 20 | + * Created by Allen on 2017/5/7. |
| 21 | + * 自定义支付密码输入框 |
| 22 | + */ |
| 23 | + |
| 24 | +public class PayPsdInputView extends EditText { |
| 25 | + |
| 26 | + private Context mContext; |
| 27 | + /** |
| 28 | + * 第一个圆开始绘制的圆心坐标 |
| 29 | + */ |
| 30 | + private float startX; |
| 31 | + private float startY; |
| 32 | + |
| 33 | + |
| 34 | + private float cX; |
| 35 | + |
| 36 | + |
| 37 | + /** |
| 38 | + * 实心圆的半径 |
| 39 | + */ |
| 40 | + private int radius = 10; |
| 41 | + /** |
| 42 | + * view的高度 |
| 43 | + */ |
| 44 | + private int height; |
| 45 | + private int width; |
| 46 | + |
| 47 | + /** |
| 48 | + * 当前输入密码位数 |
| 49 | + */ |
| 50 | + private int textLength = 0; |
| 51 | + private int bottomLineLength; |
| 52 | + /** |
| 53 | + * 最大输入位数 |
| 54 | + */ |
| 55 | + private int maxCount = 6; |
| 56 | + /** |
| 57 | + * 圆的颜色 默认BLACK |
| 58 | + */ |
| 59 | + private int circleColor = Color.BLACK; |
| 60 | + /** |
| 61 | + * 底部线的颜色 默认GRAY |
| 62 | + */ |
| 63 | + private int bottomLineColor = Color.GRAY; |
| 64 | + |
| 65 | + /** |
| 66 | + * 分割线的颜色 |
| 67 | + */ |
| 68 | + private int borderColor = Color.GRAY; |
| 69 | + /** |
| 70 | + * 分割线的画笔 |
| 71 | + */ |
| 72 | + private Paint borderPaint; |
| 73 | + /** |
| 74 | + * 分割线开始的坐标x |
| 75 | + */ |
| 76 | + private int divideLineWStartX; |
| 77 | + |
| 78 | + /** |
| 79 | + * 分割线的宽度 默认2 |
| 80 | + */ |
| 81 | + private int divideLineWidth = 2; |
| 82 | + /** |
| 83 | + * 竖直分割线的颜色 |
| 84 | + */ |
| 85 | + private int divideLineColor = Color.GRAY; |
| 86 | + private RectF rectF = new RectF(); |
| 87 | + |
| 88 | + private int psdType = 0; |
| 89 | + private final static int psdType_weChat = 0; |
| 90 | + private final static int psdType_bottomLine = 1; |
| 91 | + |
| 92 | + /** |
| 93 | + * 矩形边框的圆角 |
| 94 | + */ |
| 95 | + private int rectAngle = 0; |
| 96 | + /** |
| 97 | + * 竖直分割线的画笔 |
| 98 | + */ |
| 99 | + private Paint divideLinePaint; |
| 100 | + /** |
| 101 | + * 圆的画笔 |
| 102 | + */ |
| 103 | + private Paint circlePaint; |
| 104 | + /** |
| 105 | + * 底部线的画笔 |
| 106 | + */ |
| 107 | + private Paint bottomLinePaint; |
| 108 | + |
| 109 | + /** |
| 110 | + * 需要对比的密码 一般为上次输入的 |
| 111 | + */ |
| 112 | + private String mComparePassword = null; |
| 113 | + |
| 114 | + private onPasswordListener mListener; |
| 115 | + |
| 116 | + public PayPsdInputView(Context context, AttributeSet attrs) { |
| 117 | + super(context, attrs); |
| 118 | + mContext = context; |
| 119 | + |
| 120 | + getAtt(attrs); |
| 121 | + initPaint(); |
| 122 | + |
| 123 | + this.setBackgroundColor(Color.TRANSPARENT); |
| 124 | + this.setCursorVisible(false); |
| 125 | + this.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxCount)}); |
| 126 | + |
| 127 | + } |
| 128 | + |
| 129 | + private void getAtt(AttributeSet attrs) { |
| 130 | + TypedArray typedArray = mContext.obtainStyledAttributes(attrs, R.styleable.PayPsdInputView); |
| 131 | + maxCount = typedArray.getInt(R.styleable.PayPsdInputView_maxCount, maxCount); |
| 132 | + circleColor = typedArray.getColor(R.styleable.PayPsdInputView_circleColor, circleColor); |
| 133 | + bottomLineColor = typedArray.getColor(R.styleable.PayPsdInputView_bottomLineColor, bottomLineColor); |
| 134 | + radius = typedArray.getDimensionPixelOffset(R.styleable.PayPsdInputView_radius, radius); |
| 135 | + |
| 136 | + divideLineWidth = typedArray.getDimensionPixelSize(R.styleable.PayPsdInputView_divideLineWidth, divideLineWidth); |
| 137 | + divideLineColor = typedArray.getColor(R.styleable.PayPsdInputView_divideLineColor, divideLineColor); |
| 138 | + psdType = typedArray.getInt(R.styleable.PayPsdInputView_psdType, psdType); |
| 139 | + rectAngle = typedArray.getDimensionPixelOffset(R.styleable.PayPsdInputView_rectAngle, rectAngle); |
| 140 | + |
| 141 | + typedArray.recycle(); |
| 142 | + } |
| 143 | + |
| 144 | + /** |
| 145 | + * 初始化画笔 |
| 146 | + */ |
| 147 | + private void initPaint() { |
| 148 | + |
| 149 | + circlePaint = getPaint(5, Paint.Style.FILL, circleColor); |
| 150 | + |
| 151 | + bottomLinePaint = getPaint(2, Paint.Style.FILL, bottomLineColor); |
| 152 | + |
| 153 | + borderPaint = getPaint(3, Paint.Style.STROKE, borderColor); |
| 154 | + |
| 155 | + divideLinePaint = getPaint(divideLineWidth, Paint.Style.FILL, borderColor); |
| 156 | + |
| 157 | + } |
| 158 | + |
| 159 | + /** |
| 160 | + * 设置画笔 |
| 161 | + * |
| 162 | + * @param strokeWidth 画笔宽度 |
| 163 | + * @param style 画笔风格 |
| 164 | + * @param color 画笔颜色 |
| 165 | + * @return |
| 166 | + */ |
| 167 | + private Paint getPaint(int strokeWidth, Paint.Style style, int color) { |
| 168 | + Paint paint = new Paint(ANTI_ALIAS_FLAG); |
| 169 | + paint.setStrokeWidth(strokeWidth); |
| 170 | + paint.setStyle(style); |
| 171 | + paint.setColor(color); |
| 172 | + paint.setAntiAlias(true); |
| 173 | + |
| 174 | + return paint; |
| 175 | + } |
| 176 | + |
| 177 | + |
| 178 | + @Override |
| 179 | + protected void onSizeChanged(int w, int h, int oldw, int oldh) { |
| 180 | + super.onSizeChanged(w, h, oldw, oldh); |
| 181 | + height = h; |
| 182 | + width = w; |
| 183 | + |
| 184 | + divideLineWStartX = w / maxCount; |
| 185 | + |
| 186 | + startX = w / maxCount / 2; |
| 187 | + startY = h / 2; |
| 188 | + |
| 189 | + bottomLineLength = w / (maxCount + 2); |
| 190 | + |
| 191 | + rectF.set(0, 0, width, height); |
| 192 | + |
| 193 | + } |
| 194 | + |
| 195 | + @Override |
| 196 | + protected void onDraw(Canvas canvas) { |
| 197 | + //不删除的画会默认绘制输入的文字 |
| 198 | +// super.onDraw(canvas); |
| 199 | + |
| 200 | + switch (psdType) { |
| 201 | + case psdType_weChat: |
| 202 | + drawWeChatBorder(canvas); |
| 203 | + break; |
| 204 | + case psdType_bottomLine: |
| 205 | + drawBottomBorder(canvas); |
| 206 | + break; |
| 207 | + } |
| 208 | + |
| 209 | + drawPsdCircle(canvas); |
| 210 | + } |
| 211 | + |
| 212 | + |
| 213 | + /** |
| 214 | + * 画微信支付密码的样式 |
| 215 | + * |
| 216 | + * @param canvas |
| 217 | + */ |
| 218 | + private void drawWeChatBorder(Canvas canvas) { |
| 219 | + |
| 220 | + canvas.drawRoundRect(rectF, rectAngle, rectAngle, borderPaint); |
| 221 | + |
| 222 | + for (int i = 0; i < maxCount - 1; i++) { |
| 223 | + canvas.drawLine((i + 1) * divideLineWStartX, |
| 224 | + 0, |
| 225 | + (i + 1) * divideLineWStartX, |
| 226 | + height, |
| 227 | + divideLinePaint); |
| 228 | + } |
| 229 | + |
| 230 | + } |
| 231 | + |
| 232 | + |
| 233 | + /** |
| 234 | + * 画底部显示的分割线 |
| 235 | + * |
| 236 | + * @param canvas |
| 237 | + */ |
| 238 | + private void drawBottomBorder(Canvas canvas) { |
| 239 | + |
| 240 | + for (int i = 0; i < maxCount; i++) { |
| 241 | + cX = startX + i * 2 * startX; |
| 242 | + canvas.drawLine(cX - bottomLineLength / 2, |
| 243 | + height, |
| 244 | + cX + bottomLineLength / 2, |
| 245 | + height, bottomLinePaint); |
| 246 | + } |
| 247 | + } |
| 248 | + |
| 249 | + /** |
| 250 | + * 画密码实心圆 |
| 251 | + * |
| 252 | + * @param canvas |
| 253 | + */ |
| 254 | + private void drawPsdCircle(Canvas canvas) { |
| 255 | + for (int i = 0; i < textLength; i++) { |
| 256 | + canvas.drawCircle(startX + i * 2 * startX, |
| 257 | + startY, |
| 258 | + radius, |
| 259 | + circlePaint); |
| 260 | + } |
| 261 | + } |
| 262 | + |
| 263 | + @Override |
| 264 | + protected void onTextChanged(CharSequence text, int start, int lengthBefore, int lengthAfter) { |
| 265 | + super.onTextChanged(text, start, lengthBefore, lengthAfter); |
| 266 | + textLength = text.toString().length(); |
| 267 | + |
| 268 | + if (mComparePassword != null && textLength == maxCount) { |
| 269 | + if (TextUtils.equals(mComparePassword, getPasswordString())) { |
| 270 | + mListener.onEqual(getPasswordString()); |
| 271 | + } else { |
| 272 | + mListener.onDifference(); |
| 273 | + } |
| 274 | + } |
| 275 | + invalidate(); |
| 276 | + |
| 277 | + } |
| 278 | + |
| 279 | + @Override |
| 280 | + protected void onSelectionChanged(int selStart, int selEnd) { |
| 281 | + super.onSelectionChanged(selStart, selEnd); |
| 282 | + |
| 283 | + //保证光标始终在最后 |
| 284 | + if (selStart == selEnd) { |
| 285 | + setSelection(getText().length()); |
| 286 | + } |
| 287 | + } |
| 288 | + |
| 289 | + /** |
| 290 | + * 获取输入的密码 |
| 291 | + * |
| 292 | + * @return |
| 293 | + */ |
| 294 | + public String getPasswordString() { |
| 295 | + return getText().toString().trim(); |
| 296 | + } |
| 297 | + |
| 298 | + /** |
| 299 | + * 密码比较监听 |
| 300 | + */ |
| 301 | + public interface onPasswordListener { |
| 302 | + void onDifference(); |
| 303 | + |
| 304 | + void onEqual(String psd); |
| 305 | + } |
| 306 | + |
| 307 | + public void setComparePassword(String comparePassword, onPasswordListener listener) { |
| 308 | + mComparePassword = comparePassword; |
| 309 | + mListener = listener; |
| 310 | + } |
| 311 | +} |