public MaskFormatWatcher(MaskImpl maskOriginal) { setMask(maskOriginal); }
public void setMask(MaskImpl maskOriginal) { this.maskOriginal = maskOriginal; refreshMask(); }
@SuppressLint("SetTextI18n") @Test public void cursorPosition() { MaskFormatWatcher watcher = new MaskFormatWatcher(MaskImpl.createTerminated(PredefinedSlots.RUS_PHONE_NUMBER)); assertEquals(0, watcher.getCursorPosition()); TextView textView = new TextView(RuntimeEnvironment.application); watcher.installOn(textView); textView.setText("9"); // +7_(9 assertEquals(5, watcher.getCursorPosition()); textView.setText(null); assertEquals(0, watcher.getCursorPosition()); textView.setText("999"); // +7_(999)_ assertEquals(9, watcher.getCursorPosition()); textView.setText("999876"); // +7_(999)_876- assertEquals(13, watcher.getCursorPosition()); textView.setText("9998765432111111111"); // +7_(999)_876-54-32 assertEquals(18, watcher.getCursorPosition()); textView.setText("7"); // +7_( assertEquals(4, watcher.getCursorPosition()); }
private void init(int editTextId, ExampleBehaviour behaviour) { EditText editText = (EditText) findViewById(editTextId); Slot[] slots = Slot.copySlotArray(PredefinedSlots.RUS_PHONE_NUMBER); behaviour.changeSlots(slots); MaskImpl mask = new MaskImpl(slots, behaviour.isTerminated()); behaviour.changeMask(mask); MaskFormatWatcher watcher = new MaskFormatWatcher(mask); if (behaviour.fillWhenInstall()) { watcher.installOnAndFill(editText); } else { watcher.installOn(editText); } }
@Override protected void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_custom_mask); EditText maskEdit = (EditText) findViewById(R.id.editMask); maskEdit.addTextChangedListener(maskTextWatcher); formatWatcher = new MaskFormatWatcher(createEmptyMask()); dataEdit = (EditText) findViewById(R.id.editData); formatWatcher.installOn(dataEdit); }
@Test public void getUnformattedString() { MaskFormatWatcher watcher = new MaskFormatWatcher(MaskImpl.createTerminated(PredefinedSlots.RUS_PHONE_NUMBER)); assertEquals("+7", watcher.getMask().toUnformattedString()); }
@Test public void forbidCursorMoving() { MaskFormatWatcher watcher = new MaskFormatWatcher(new MaskImpl(new PhoneNumberUnderscoreSlotsParser().parseSlots("+7 (___) ___-__-__"), true)); assertEquals(0, watcher.getCursorPosition()); TextView textView = new TextView(RuntimeEnvironment.application); watcher.installOn(textView); textView.setText("9"); // +7_(9 assertEquals(5, watcher.getCursorPosition()); textView.setText(null); assertEquals(0, watcher.getCursorPosition()); textView.setText("999"); // +7_(999)_ assertEquals(9, watcher.getCursorPosition()); textView.setText("999876"); // +7_(999)_876- assertEquals(13, watcher.getCursorPosition()); textView.setText("9998765432111111111"); // +7_(999)_876-54-32 assertEquals(18, watcher.getCursorPosition()); textView.setText("7"); // +7_( assertEquals(4, watcher.getCursorPosition()); } }
@Test public void replace() { MaskFormatWatcher watcher = new MaskFormatWatcher(MaskImpl.createTerminated(PredefinedSlots.RUS_PHONE_NUMBER)); TextView textView = new TextView(RuntimeEnvironment.application); watcher.installOn(textView); textView.setText("+79990000000"); { Editable editable = (Editable) textView.getText(); editable.replace(4, 18, "111"); assertEquals("+7 (111) ", textView.getText().toString()); } { Editable editable = (Editable) textView.getText(); editable.replace(5, 6, "222"); assertEquals("+7 (122) 21", textView.getText().toString()); } textView.setText("+79991234567"); { Editable editable = (Editable) textView.getText(); editable.replace(6, 7, ""); assertEquals("+7 (991) 234-56-7", textView.getText().toString()); } textView.setText("+712345"); { Editable editable = (Editable) textView.getText(); editable.replace(4, 11, "123456"); assertEquals("+7 (123) 456-", textView.getText().toString()); } }
@Override public void afterTextChanged(Editable s) { final MaskImpl maskDescriptor; if (TextUtils.isEmpty(s)) { maskDescriptor = createEmptyMask(); } else { maskDescriptor = MaskImpl.createTerminated(slotsParser.parseSlots(s.toString())); maskDescriptor.setHideHardcodedHead(false); maskDescriptor.insertFront(dataEdit.getText().toString()); } formatWatcher.setMask(maskDescriptor); } };
public void swapMask(MaskImpl newMask) { maskOriginal = new MaskImpl(newMask); maskOriginal.clear(); refreshMask(newMask.toString()); } }