|
2 | 2 |
|
3 | 3 | /** |
4 | 4 | * Created by Swapnil Tiwari on 2019-05-07. |
5 | | - * swapnil.tiwari@box8.in |
| 5 | + * swapniltiwari775@gmail.com |
6 | 6 | */ |
7 | 7 |
|
8 | 8 | import android.content.Context; |
|
12 | 12 | import android.support.v4.content.ContextCompat; |
13 | 13 | import android.support.v7.widget.AppCompatEditText; |
14 | 14 | import android.text.Editable; |
15 | | -import android.text.method.PasswordTransformationMethod; |
| 15 | +import android.text.TextWatcher; |
16 | 16 | import android.util.AttributeSet; |
17 | 17 | import android.view.ActionMode; |
18 | 18 | import android.view.Menu; |
19 | 19 | import android.view.MenuItem; |
20 | 20 | import android.view.View; |
21 | 21 |
|
22 | 22 |
|
23 | | -public class OtpEditText extends AppCompatEditText { |
| 23 | +public class OtpEditText extends AppCompatEditText implements TextWatcher { |
24 | 24 | public static final String XML_NAMESPACE_ANDROID = "http://schemas.android.com/apk/res/android"; |
25 | 25 |
|
26 | 26 | private OnClickListener mClickListener; |
@@ -72,6 +72,9 @@ private void init(Context context, AttributeSet attrs) { |
72 | 72 |
|
73 | 73 | getAttrsFromTypedArray(attrs); |
74 | 74 |
|
| 75 | + // Set the TextWatcher |
| 76 | + this.addTextChangedListener(this); |
| 77 | + |
75 | 78 | float multi = context.getResources().getDisplayMetrics().density; |
76 | 79 | mLineStroke = multi * mLineStroke; |
77 | 80 | mLineStrokeSelected = multi * mLineStrokeSelected; |
@@ -230,11 +233,6 @@ protected void onDraw(Canvas canvas) { |
230 | 233 | } else { |
231 | 234 | canvas.drawText(text, i, i + 1, middle - textWidths[0] / 2, mLineSpacing, getPaint()); |
232 | 235 | } |
233 | | - if (i + 1 == mNumChars) { |
234 | | - if (completeListener != null) { |
235 | | - completeListener.onComplete(String.valueOf(getText())); |
236 | | - } |
237 | | - } |
238 | 236 | } |
239 | 237 |
|
240 | 238 | if (mSpace < 0) { |
@@ -274,4 +272,19 @@ private void updateColorForLines(boolean next, boolean current) { |
274 | 272 | public void setOnCompleteListener(OnCompleteListener listener) { |
275 | 273 | completeListener = listener; |
276 | 274 | } |
| 275 | + |
| 276 | + @Override |
| 277 | + public void beforeTextChanged(CharSequence s, int start, int count, int after) { |
| 278 | + } |
| 279 | + |
| 280 | + @Override |
| 281 | + public void onTextChanged(CharSequence s, int start, int before, int count) { |
| 282 | + } |
| 283 | + |
| 284 | + @Override |
| 285 | + public void afterTextChanged(Editable s) { |
| 286 | + if (s.length() == mNumChars) { |
| 287 | + completeListener.onComplete(String.valueOf(s)); |
| 288 | + } |
| 289 | + } |
277 | 290 | } |
0 commit comments