本文实例为大家分享了Android自定义View验证码输入框的具体代码,供大家参考,具体内容如下
验证码输入框
1.先看下样式
2.直接上代码
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
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
|
public class MyVcode extends AppCompatEditText { private int mFigures = 0 ; // 验证码个数 private int mCodeMargin = 0 ; // 验证码之间的间距 private int mSelectColor = 0 ; // 选中框的颜色 private int mNormalColor = 0 ; // 普通框的颜色 private float mBorderWidth = 0f; // 边框的厚度 Paint mNormalPaint = new Paint(); Paint mSelectPaint = new Paint(); Paint mOkContentPain = new Paint(); //填写了内容的验证码 private OnVerifyCodeChangedListener onVerifyCodeChangedListener; private int mCurrentPosition = 0 ; // 当前验证码的位置 private int mEachRectLength = 0 ; // 矩形边长 public MyVcode(Context context) { super (context, null ); } public MyVcode(Context context, AttributeSet attrs) { super (context, attrs); initAttr(context,attrs); initPaint(); setFocusableInTouchMode( true ); initTextChangeListerner(); } private void initTextChangeListerner() { addTextChangedListener( new TextWatcher() { @Override public void beforeTextChanged(CharSequence s, int start, int count, int after) { mCurrentPosition = getText().length(); } @Override public void onTextChanged(CharSequence s, int start, int before, int count) { mCurrentPosition = getText().length(); postInvalidate(); if (onVerifyCodeChangedListener!= null ){ onVerifyCodeChangedListener.onVerCodeChanged(getText().toString(),start,before,count); } } @Override public void afterTextChanged(Editable s) { mCurrentPosition = getText().length(); postInvalidate(); if (getText().length() == mFigures){ if (onVerifyCodeChangedListener!= null ){ onVerifyCodeChangedListener.onInputCompleted(getText().toString()); } } else if (getText().length()>mFigures){ getText().delete(mFigures,getText().length()); } } }); } @Override public boolean onTouchEvent(MotionEvent event) { if (event.getAction()==MotionEvent.ACTION_DOWN){ requestFocus(); setSelection(getText().length()); showKeyBoard(getContext()); return false ; } return super .onTouchEvent(event); } private void showKeyBoard( Context context) { InputMethodManager imm = (InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput( this , InputMethodManager.SHOW_FORCED); } private void initAttr(Context context, AttributeSet attrs) { TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.VerifyCodeEditText); mFigures = ta.getInteger(R.styleable.VerifyCodeEditText_figures, 6 ); mCodeMargin = ( int ) ta.getDimension(R.styleable.VerifyCodeEditText_codeMargin, 0f); mSelectColor = ta.getColor(R.styleable.VerifyCodeEditText_selectBorderColor,getResources().getColor(R.color.base_barcolor)); mNormalColor = ta.getColor( R.styleable.VerifyCodeEditText_normalBorderColor, getResources().getColor(android.R.color.darker_gray) ); mBorderWidth = ta.getDimension(R.styleable.VerifyCodeEditText_borderWidth, 1f); /* mCursorColor = ta.getColor( R.styleable.VerifyCodeEditText_cursorColor, getResources().getColor(android.R.color.darker_gray) ); ta.recycle();*/ /* // force LTR because of bug: https://github.com/JustKiddingBaby/VercodeEditText/issues/4 if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN_MR1) { layoutDirection = LAYOUT_DIRECTION_LTR }*/ } private void initPaint() { mNormalPaint.setAntiAlias( true ); mNormalPaint.setColor(mNormalColor); mNormalPaint.setStyle(Paint.Style.FILL_AND_STROKE); mNormalPaint.setStrokeWidth(mBorderWidth); mSelectPaint.setAntiAlias( true ); mSelectPaint.setColor(mSelectColor); mSelectPaint.setStyle(Paint.Style.STROKE); mSelectPaint.setStrokeWidth(mBorderWidth); mOkContentPain.setAntiAlias( true ); mOkContentPain.setColor(mSelectColor); mOkContentPain.setStyle(Paint.Style.FILL_AND_STROKE); mOkContentPain.setStrokeWidth(mBorderWidth); } @Override protected void onMeasure( int widthMeasureSpec, int heightMeasureSpec) { super .onMeasure(widthMeasureSpec, heightMeasureSpec); int size = MeasureSpec.getSize(heightMeasureSpec); // 每个矩形的宽度 相当于我门圆的直径 mEachRectLength = size ; } @Override protected void onDraw(Canvas canvas) { //super.onDraw(canvas); Editable text = this .getText(); mCurrentPosition = text.length(); int width = mEachRectLength -getPaddingLeft() - getPaddingRight(); for ( int i = 0 ;i< mFigures;i++){ canvas.save(); float start = width*i +mCodeMargin*i+mBorderWidth; float end = start+width - mBorderWidth; if (i==mFigures- 1 ){ end -= mBorderWidth; } RectF rect = new RectF(start, mBorderWidth, end, width - mBorderWidth); //画底色 canvas.drawArc(rect, 0f, 360f, true , mNormalPaint); if (i == mCurrentPosition) { //选中的下一个状态 canvas.drawArc(rect, 0f, 360f, true , mNormalPaint); canvas.drawArc(rect, 0f, 360f, true , mSelectPaint); } else { canvas.drawArc(rect, 0f, 360f, true , mNormalPaint); } if (mCurrentPosition>i){ //画已经有内容的 canvas.drawArc(rect, 0f, 360f, true , mOkContentPain); } canvas.restore(); } String s = text.toString(); //画文字 for ( int i = 0 ;i<mCurrentPosition;i++){ canvas.save(); int start = width * i + mCodeMargin * i; float x = start + width / 2f; TextPaint paint = getPaint(); paint.setTextAlign(Paint.Align.CENTER); paint.setColor(getCurrentTextColor()); Paint.FontMetrics fontMetrics = paint.getFontMetrics(); float baseline = (width - fontMetrics.bottom + fontMetrics.top) / 2 - fontMetrics.top; canvas.drawText(s.substring(i,i+ 1 ), x, baseline, paint); canvas.restore(); } } /** * 验证码变化时候的监听事件 */ interface OnVerifyCodeChangedListener { /** * 当验证码变化的时候 */ public void onVerCodeChanged(String s, int start, int before, int count); /** * 输入完毕后的回调 */ public void onInputCompleted(String s); } } |
3.自定义的属性
1
2
3
4
5
6
7
8
9
|
<!--验证码的属性--> < declare-styleable name = "VerifyCodeEditText" > < attr name = "figures" format = "integer" /> <!--验证码的个数--> < attr name = "codeMargin" format = "dimension" /> <!--验证码的之间的间隔--> < attr name = "selectBorderColor" format = "color|reference" /> <!--选中的边框颜色--> < attr name = "normalBorderColor" format = "color|reference" /> <!--普通的边框颜色--> < attr name = "borderWidth" format = "dimension" /> <!--边框的厚度 --> < attr name = "cursorColor" format = "color|reference" /> <!--已经有内容的颜色 --> </ declare-styleable > |
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持服务器之家。
原文链接:https://blog.csdn.net/LDH_123/article/details/105583271