Skip to content

Commit 5c9d92e

Browse files
authored
Merge branch 'master' into feature/switch-to-coroutines
2 parents 50710f2 + d5506a7 commit 5c9d92e

File tree

3 files changed

+74
-26
lines changed

3 files changed

+74
-26
lines changed

MaskedEditText/src/main/java/br/com/sapereaude/maskedEditText/MaskedEditText.java

Lines changed: 47 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,19 @@
2525
public 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() {

MaskedEditText/src/main/res/values/attrs.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
<declare-styleable name="MaskedEditText">
44
<attr name="mask" format="string" />
55
<attr name="allowed_chars" format="string" />
6+
<attr name="enable_ime_action" format="boolean" />
67
<attr name="denied_chars" format="string" />
78
<attr name="char_representation" format="string" />
89
<attr name="keep_hint" format="boolean" />

README.md

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,20 @@ You can also change the mask and the representation character programatically:
6666
editText.setMask("##/##/####");
6767
// Logging the mask
6868
Log.i("Mask", editText.getMask());
69+
70+
To enable Enter softkey action (IME action):
6971

72+
<br.com.sapereaude.maskedEditText.MaskedEditText
73+
...
74+
mask:enable_ime_action="true"
75+
...
76+
/>
77+
78+
Or programmatically:
79+
80+
MaskedEditText editText = (MaskedEditText) findViewById(R.id.my_edit_text)
81+
editText.setImeActionEnabled(true);
82+
7083
*************************************************************************************************
7184
## ru_RU
7285

@@ -118,3 +131,16 @@ _mask_ задаёт требуемую маску, символ '#' задаёт
118131
editText.setMask("##/##/####");
119132
// Logging the mask
120133
Log.i("Mask", editText.getMask());
134+
135+
Чтобы включить обработку нажатия Enter (IME action):
136+
137+
<br.com.sapereaude.maskedEditText.MaskedEditText
138+
...
139+
mask:enable_ime_action="true"
140+
...
141+
/>
142+
143+
Или программно:
144+
145+
MaskedEditText editText = (MaskedEditText) findViewById(R.id.my_edit_text)
146+
editText.setImeActionEnabled(true);

0 commit comments

Comments
 (0)