2525public class MaskedEditText extends AppCompatEditText implements TextWatcher {
2626
2727 public static final String SPACE = " " ;
28- private String mask ;
28+ private final OnEditorActionListener onEditorActionListener = new OnEditorActionListener () {
29+ @ Override
30+ public boolean onEditorAction (TextView v , int actionId ,KeyEvent event ) {
31+ switch (actionId ) {
32+ // case EditorInfo.IME_ACTION_NEXT:
33+ // fixing actionNext
34+ // return false;
35+ default :
36+ return true ;
37+ }
38+ }
39+ };
40+ private String mask ;
2941 private char charRepresentation ;
3042 private boolean keepHint ;
3143 private int [] rawToMask ;
@@ -41,11 +53,11 @@ public class MaskedEditText extends AppCompatEditText implements TextWatcher {
4153 private int lastValidMaskPosition ;
4254 private boolean selectionChanged ;
4355 private OnFocusChangeListener focusChangeListener ;
44- private String allowedChars ;
45- private String deniedChars ;
46- private BehaviorProcessor <String > rawTextState = BehaviorProcessor .create ();
56+ private String allowedChars ;
57+ private String deniedChars ;
58+ private BehaviorProcessor <String > rawTextState = BehaviorProcessor .create ();
4759 private boolean blockFurtherSelectionChanges = false ;
48-
60+
4961 public MaskedEditText (Context context ) {
5062 super (context );
5163 init ();
@@ -60,6 +72,7 @@ public MaskedEditText(Context context, AttributeSet attrs) {
6072
6173 allowedChars = attributes .getString (R .styleable .MaskedEditText_allowed_chars );
6274 deniedChars = attributes .getString (R .styleable .MaskedEditText_denied_chars );
75+ boolean enableImeAction = attributes .getBoolean (R .styleable .MaskedEditText_enable_ime_action , false );
6376
6477 String representation = attributes .getString (R .styleable .MaskedEditText_char_representation );
6578
@@ -73,19 +86,12 @@ public MaskedEditText(Context context, AttributeSet attrs) {
7386
7487 cleanUp ();
7588
76- // Ignoring enter key presses
77- setOnEditorActionListener (new OnEditorActionListener () {
78- @ Override
79- public boolean onEditorAction (TextView v , int actionId ,KeyEvent event ) {
80- switch (actionId ) {
81- // case EditorInfo.IME_ACTION_NEXT:
82- // fixing actionNext
83- // return false;
84- default :
85- return true ;
86- }
87- }
88- });
89+ // Ignoring enter key presses if needed
90+ if (!enableImeAction ) {
91+ setOnEditorActionListener (onEditorActionListener );
92+ } else {
93+ setOnEditorActionListener (null );
94+ }
8995 attributes .recycle ();
9096 }
9197
@@ -130,12 +136,14 @@ public void setOnFocusChangeListener(OnFocusChangeListener listener) {
130136
131137 private void cleanUp () {
132138 initialized = false ;
133-
139+ if (mask == null || mask .isEmpty ()){
140+ return ;
141+ }
134142 generatePositionArrays ();
135-
136- rawText = new RawText ();
137- selection = rawToMask [0 ];
138-
143+ if (! shouldKeepText || rawText == null ) {
144+ rawText = new RawText ();
145+ selection = rawToMask [0 ];
146+ }
139147 editingBefore = true ;
140148 editingOnChanged = true ;
141149 editingAfter = true ;
@@ -183,6 +191,14 @@ public MaskedEditText(Context context, AttributeSet attrs, int defStyle) {
183191 init ();
184192 }
185193
194+ public void setShouldKeepText (boolean shouldKeepText ) {
195+ this .shouldKeepText = shouldKeepText ;
196+ }
197+
198+ public boolean isKeepingText () {
199+ return shouldKeepText ;
200+ }
201+
186202 public void setMask (String mask ) {
187203 this .mask = mask ;
188204 cleanUp ();
@@ -192,6 +208,13 @@ public String getMask() {
192208 return this .mask ;
193209 }
194210
211+ public void setImeActionEnabled (boolean isEnabled ) {
212+ if (isEnabled )
213+ setOnEditorActionListener (onEditorActionListener );
214+ else
215+ setOnEditorActionListener (null );
216+ }
217+
195218 public String getRawText () {
196219 return this .rawText .getText ();
197220 }
@@ -240,9 +263,7 @@ private void generatePositionArrays() {
240263 char [] charsInMask = charsInMaskAux .toCharArray ();
241264
242265 rawToMask = new int [charIndex ];
243- for (int i = 0 ; i < charIndex ; i ++) {
244- rawToMask [i ] = aux [i ];
245- }
266+ System .arraycopy (aux , 0 , rawToMask , 0 , charIndex );
246267 }
247268
248269 private void init () {
0 commit comments